content/commands/json.set.md
Set or replace the value at each location resolved by path.
If the key does not exist, a new JSON document can be created only by setting the root path ($ or .).
JSON.SET can also create new object members when the parent object exists.
is a new key to create or an existing JSON key to modify.
</details> <details open><summary><code>path</code></summary>A JSONPath expression that resolves to zero or more locations within the JSON document.
$ or ..path resolves to one or more existing locations, the value at each matched location is replaced with value.path is a non-existing object member and the parent location exists and is an object, the member is created and set to value.Optional arguments NX and XX modify this behavior for both new keys and existing JSON keys.
A valid JSON value to set at the specified path.
The value can be a scalar (string, number, boolean, or null) or a compound value such as an object or array.
</details>Sets the value only if path has no matches.
Sets the value only if path has one or more matches.
{{< highlight bash >}} redis> JSON.SET doc $ '{"a":2}' OK redis> JSON.SET doc $.a '3' OK redis> JSON.GET doc $ "[{"a":3}]" {{< / highlight >}}
</details> <details open> <summary><b>Add a new value</b></summary>{{< highlight bash >}} redis> JSON.SET doc $ '{"a":2}' OK redis> JSON.SET doc $.b '8' OK redis> JSON.GET doc $ "[{"a":2,"b":8}]" {{< / highlight >}}
</details> <details open> <summary><b>Update multiple matches</b></summary>{{< highlight bash >}} redis> JSON.SET doc $ '{"f1": {"a":1}, "f2":{"a":2}}' OK redis> JSON.SET doc $..a 3 OK redis> JSON.GET doc "{"f1":{"a":3},"f2":{"a":3}}" {{< / highlight >}}
</details> <details open> <summary><b>path does not exist and cannot be created</b></summary>{{< highlight bash >}} redis> JSON.SET doc $ 1 OK redis> JSON.SET doc $.x.y 2 (nil) {{< / highlight >}}
</details> <details open> <summary><b>XX condition unmet</b></summary>{{< highlight bash >}} redis> JSON.SET nonexistentkey $ 5 XX (nil) redis> JSON.GET nonexistentkey (nil) {{< / highlight >}}
</details> <details open> <summary><b>key does not exist and path is not root</b></summary>{{< highlight bash >}} redis> JSON.SET nonexistentkey $.x 5 (error) ERR new objects must be created at the root {{< / highlight >}}
</details>| Redis Software | Redis Cloud | <span style="min-width: 9em; display: table-cell">Notes</span> | |:----------------------|:-----------------|:------| | <span title="Supported">✅ Supported</span> | <span title="Supported">✅ Flexible & Annual</span> <span title="Supported">✅ Free & Fixed</nobr></span> | |
{{< multitabs id="json-set-return-info" tab1="RESP2" tab2="RESP3" >}}
One of the following:
OK if executed correctly.key exists but path does not exist and cannot be created, or if an NX or XX condition is unmet.(error) expected ... - if the value is invalid.(error) Error occurred on position ... expected ... - if the path is invalid.(error) ERR new objects must be created at the root - if key does not exist and path is not root ($ or .).(error) ERR wrong static path - if a dynamic path expression has no matching locations.(error) ERR index out of bounds - if the path refers to an array index outside the array bounds.-tab-sep-
One of the following:
OK if executed correctly.key exists but path does not exist and cannot be created, or if an NX or XX condition is unmet.(error) expected ... - if the value is invalid.(error) Error occurred on position ... expected ... - if the path is invalid.(error) ERR new objects must be created at the root - if key does not exist and path is not root ($ or .).(error) ERR wrong static path - if a dynamic path expression has no matching locations.(error) ERR index out of bounds - if the path refers to an array index outside the array bounds.
{{< /multitabs >}}[JSON.GET]({{< relref "commands/json.get/" >}}) | [JSON.MGET]({{< relref "commands/json.mget/" >}})