Use JSON.SET to set a JSON value at a path inside a document, creating the key when it does not exist.
The value is JSON text; the Upstash SDK helpers serialize native objects for you. With the root path ($) the whole document is replaced, which is also how a new document is created. For a nested path the parent must already exist: the command adds one missing child to an existing object or appends to an existing array, but it does not create intermediate levels along the way.
NX writes only when the path does not exist yet and XX only when it does, which makes conditional updates atomic. When the path is a JSONPath that matches several places, every match is updated in the same call, so a single command can update all elements of an array.
Syntax#
Arguments#
| Argument | Required | Repeatable | Description |
|---|---|---|---|
key | Yes | No | JSON document key. |
path | Yes | No | Path to create or replace. |
value | Yes | No | Valid JSON value. |
NX | XX | No | No | Write only when the path is absent (NX) or present (XX). |
Important points#
- Raw TCP examples pass JSON values as valid JSON text. Typed Upstash SDK helpers serialize native objects and values for you.
- Paths beginning with
$use JSONPath and can match multiple values, so many JSON commands return an array of per-match results.
Response#
The reply reports the result of the operation. Error replies have the same shape in RESP2 and RESP3 and are surfaced as exceptions by the SDKs below.
| Protocol | Reply |
|---|---|
| RESP2 | Simple string OK, or Null bulk string or null array when NX or XX prevented the write |
| RESP3 | Simple string OK, or Null when NX or XX prevented the write |
Client libraries often decode bulk strings, maps, sets, and numeric strings into language-native values. The table describes the Redis wire reply.
Examples#
TCP examples use the TLS REDIS_URL from the Upstash console. REST examples use UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN.