---
acl_categories:
- '@json'
- '@write'
- '@slow'
arguments:
- name: key
type: key
- name: path
type: string
- name: value
type: string
- arguments:
- name: nx
token: NX
type: pure-token
- name: xx
token: XX
type: pure-token
name: condition
optional: true
type: oneof
categories:
- docs
- develop
- stack
- oss
- rs
- rc
- oss
- kubernetes
- clients
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
description: Sets or updates the JSON value at a path
group: json
hidden: false
linkTitle: JSON.SET
module: JSON
railroad_diagram: /images/railroad/json.set.svg
since: 1.0.0
stack_path: docs/data-types/json
summary: Sets or updates the JSON value at a path
syntax_fmt: JSON.SET key path value [NX | XX]
title: JSON.SET
---
Set the JSON value at `path` in `key`
[Examples](#examples)
## Required arguments
key
is key to modify.
path
is 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.
value
is value to set at the specified path
## Optional arguments
NX
sets the key only if it does not already exist.
XX
sets the key only if it already exists.
## Examples
Replace an existing value
{{< highlight bash >}}
redis> JSON.SET doc $ '{"a":2}'
OK
redis> JSON.SET doc $.a '3'
OK
redis> JSON.GET doc $
"[{\"a\":3}]"
{{< / highlight >}}
Add a new value
{{< 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 >}}
Update multi-paths
{{< 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 >}}
path does not exist and cannot be created
{{< highlight bash >}}
redis> JSON.SET doc $ 1
OK
redis> JSON.SET doc $.x.y 2
(nil)
{{< / highlight >}}
XX condition unmet
{{< highlight bash >}}
redis> JSON.SET nonexistentkey $ 5 XX
(nil)
redis> JSON.GET nonexistentkey
(nil)
{{< / highlight >}}
key does not exist and path is not root
{{< highlight bash >}}
redis> JSON.SET nonexistentkey $.x 5
(error) ERR new objects must be created at the root
{{< / highlight >}}
## Redis Software and Redis Cloud compatibility
| Redis
Software | Redis
Cloud | Notes |
|:----------------------|:-----------------|:------|
| ✅ Supported
| ✅ Flexible & Annual
✅ Free & Fixed | |
## Return information
{{< multitabs id="json-set-return-info"
tab1="RESP2"
tab2="RESP3" >}}
One of the following:
* [Simple string reply]({{< relref "/develop/reference/protocol-spec#simple-strings" >}}): `OK` if executed correctly.
* [Null reply]({{< relref "/develop/reference/protocol-spec#nulls" >}}): if `key` exists but `path` does not exist and cannot be created, or if an `NX` or `XX` condition is unmet.
* [Simple error reply]({{< relref "/develop/reference/protocol-spec#simple-errors" >}}): if `key` does not exist and `path` is not root (`.` or `$`).
-tab-sep-
One of the following:
* [Simple string reply]({{< relref "/develop/reference/protocol-spec#simple-strings" >}}): `OK` if executed correctly.
* [Null reply]({{< relref "/develop/reference/protocol-spec#nulls" >}}): if `key` exists but `path` does not exist and cannot be created, or if an `NX` or `XX` condition is unmet.
* [Simple error reply]({{< relref "/develop/reference/protocol-spec#simple-errors" >}}): if `key` does not exist and `path` is not root (`.` or `$`).
{{< /multitabs >}}
## See also
[`JSON.GET`]({{< relref "commands/json.get/" >}}) | [`JSON.MGET`]({{< relref "commands/json.mget/" >}})
## Related topics
* [RedisJSON]({{< relref "/develop/data-types/json/" >}})
* [Index and search JSON documents]({{< relref "/develop/ai/search-and-query/indexing/" >}})