JSON.MERGE
JSON.MERGE key path value
- Available in:
- Redis Stack / JSON 2.6.0
- Time complexity:
- O(M+N) when path is evaluated to a single value where M is the size of the original value (if it exists) and N is the size of the new value, O(M+N) when path is evaluated to multiple values where M is the size of the key and N is the size of the new value * the number of original values in the key
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
Required arguments
key
is key to merge into.
path
is JSONPath to specify. For non-existing keys the path
must be $
. For existing keys, for each matched path
, the value that matches the path
is being 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
.
value
is 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:
- merging an existing object key with a
null
value deletes the key - merging an existing object key with non-null value updates the value
- merging a non-existing object key adds the key and value
- merging an existing array with any merged value, replaces the entire array with the value
Return value
JSET.MERGE returns a simple string reply: OK
if executed correctly or error
if fails to set the new values
For more information about replies, see Redis serialization protocol specification.
Examples
The JSON.MERGE provide four different behaviours to merge changes on a given key: create unexistent path, update an existing path with a new value, delete a existing path or replace an array with a new array
Create a unexistent path-value
redis> JSON.SET doc $ '{"a":2}'
OK
redis> JSON.MERGE doc $.b '8'
OK
redis> JSON.GET doc $
"[{\"a\":2,\"b\":8}]"
Replace an existing value
redis> JSON.SET doc $ '{"a":2}'
OK
redis> JSON.MERGE doc $.a '3'
OK
redis> JSON.GET doc $
"[{\"a\":3}]"
Delete on existing value
redis> JSON.SET doc $ '{"a":2}'
OK
redis> JSON.MERGE doc $ '{"a":null}'
OK
redis> JSON.GET doc $
"[{}]"
Replace an Array
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]}]"
Merge changes in multi-paths
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]}"
See also
JSON.GET
| JSON.MGET
| JSON.SET
| JSON.MSET