JSON.SET key path value [NX | XX]
@json,
@write,
@slow,
Set the JSON value at path in key
keyis key to modify.
pathis JSONPath to specify. Default is root $. For new Redis keys the path must be the root. For existing keys, when the entire path exists, the value that it contains is replaced 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.
Adds a key (with its respective value) to a JSON Object (in a RedisJSON data type key) only if it is the last child in the path, or it is the parent of a new child being added in the path. Optional arguments NX and XX modify this behavior for both new RedisJSON data type keys as well as the JSON Object keys in them.
valueis value to set at the specified path
NXsets the key only if it does not already exist.
XXsets the key only if it already exists.
redis> JSON.SET doc $ '{"a":2}'
OK
redis> JSON.SET doc $.a '3'
OK
redis> JSON.GET doc $
"[{\"a\":3}]"redis> JSON.SET doc $ '{"a":2}'
OK
redis> JSON.SET doc $.b '8'
OK
redis> JSON.GET doc $
"[{\"a\":2,\"b\":8}]"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}}"redis> JSON.SET doc $ 1
OK
redis> JSON.SET doc $.x.y 2
(nil)redis> JSON.SET nonexistentkey $ 5 XX
(nil)
redis> JSON.GET nonexistentkey
(nil)redis> JSON.SET nonexistentkey $.x 5
(error) ERR new objects must be created at the root| Redis Enterprise |
Redis Cloud |
Notes |
|---|---|---|
| ✅ Supported |
✅ Flexible & Annual ✅ Free & Fixed |
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.key does not exist and path is not root (. or $).