content/commands/json.merge.md
Merge a given JSON value into matching paths. Consequently, JSON values at matching paths are updated, deleted, or expanded with new children.
This command complies with RFC7396 Json Merge Patch
is the key to merge into.
</details> <details open><summary><code>path</code></summary>specifies the JSONPath. For non-existing keys the path must be $. For existing keys, for each matched path, the value that matches the path is merged with the JSON value. For existing keys, when the path exists, except for the last element, a new child is added with the JSON value.
is the JSON value to merge with at the specified path. Merging is done according to the following rules per JSON value in the value argument while considering the corresponding original value if it exists:
null value deletes the keyJSON.MERGE provides four different behaviors to merge changes on a given key: create a non-existent path, update an existing path with a new value, delete an existing path, or replace an array with a new array
<details open> <summary><b>Create a non-existent path-value</b></summary>{{< highlight bash >}} redis> JSON.SET doc $ '{"a":2}' OK redis> JSON.MERGE doc $.b '8' OK redis> JSON.GET doc $ "[{"a":2,"b":8}]" {{< / highlight >}}
</details> <details open> <summary><b>Replace an existing value</b></summary>{{< highlight bash >}} redis> JSON.SET doc $ '{"a":2}' OK redis> JSON.MERGE doc $.a '3' OK redis> JSON.GET doc $ "[{"a":3}]" {{< / highlight >}}
</details> <details open> <summary><b>Delete an existing value</b></summary>{{< highlight bash >}} redis> JSON.SET doc $ '{"a":2}' OK redis> JSON.MERGE doc $ '{"a":null}' OK redis> JSON.GET doc $ "[{}]" {{< / highlight >}}
</details> <details open> <summary><b>Replace an Array</b></summary>{{< highlight bash >}} redis> JSON.SET doc $ '{"a":[2,4,6,8]}' OK redis> JSON.MERGE doc $.a '[10,12]' OK redis> JSON.GET doc $ "[{"a":[10,12]}]" {{< / highlight >}}
</details> <details open> <summary><b>Merge changes in multi-paths</b></summary>{{< highlight bash >}} redis> JSON.SET doc $ '{"f1": {"a":1}, "f2":{"a":2}}' OK redis> JSON.GET doc "{"f1":{"a":1},"f2":{"a":2}}" redis> JSON.MERGE doc $ '{"f1": null, "f2":{"a":3, "b":4}, "f3":[2,4,6]}' OK redis> JSON.GET doc "{"f2":{"a":3,"b":4},"f3":[2,4,6]}" {{< / highlight >}}
</details>| Redis Software | Redis Cloud | <span style="min-width: 9em; display: table-cell">Notes</span> | |:----------------------|:-----------------|:------| | <span title="Supported">✅ Standard</span> <span title="Supported"><nobr>✅ Active-Active</nobr></span> | <span title="Supported">✅ Standard</span> <span title="Supported"><nobr>✅ Active-Active</nobr></span> | |
{{< multitabs id="json-merge-return-info" tab1="RESP2" tab2="RESP3" >}}
One of the following:
OK if executed correctly.-tab-sep-
One of the following:
OK if executed correctly.{{< /multitabs >}}
[JSON.GET]({{< relref "commands/json.get/" >}}) | [JSON.MGET]({{< relref "commands/json.mget/" >}}) | [JSON.SET]({{< relref "commands/json.set/" >}}) | [JSON.MSET]({{< relref "commands/json.mset/" >}})