{
  "id": "json.set",
  "title": "JSON.SET",
  "url": "https://redis.io/docs/latest/commands/json.set/",
  "summary": "Sets or updates the JSON value at a path",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-04-09T10:29:34-04:00",
  "page_type": "content",
  "content_hash": "0b0c1463ba1542f3fd523e488ee064bbc2a3903d4e5cde00e5aa2b5aeec2ac55",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "Set or replace the value at each location resolved by `path`.\n\nIf the key does not exist, a new JSON document can be created only by setting the root path (`$` or `.`).\n\n`JSON.SET` can also create new object members when the parent object exists.\n\n[Examples](#examples)"
    },
    {
      "id": "required-arguments",
      "title": "Required arguments",
      "role": "content",
      "text": "<details open><summary><code>key</code></summary> \n\nis a new key to create or an existing JSON key to modify.\n</details>\n\n<details open><summary><code>path</code></summary> \n\nA JSONPath expression that resolves to zero or more locations within the JSON document.\n\n- The root of the document is specified as `$` or `.`.\n- If `path` resolves to one or more existing locations, the value at each matched location is replaced with `value`.\n- If the final token of `path` is a non-existing object member and the parent location exists and is an object, the member is created and set to `value`.\n- If any intermediate path element does not exist, the path cannot be created, and the command returns nil.\n\nOptional arguments `NX` and `XX` modify this behavior for both new keys and existing JSON keys.\n</details>\n\n<details open><summary><code>value</code></summary> \n\nA valid JSON value to set at the specified path.\n\nThe value can be a scalar (string, number, boolean, or null) or a compound value such as an object or array.\n</details>"
    },
    {
      "id": "optional-arguments",
      "title": "Optional arguments",
      "role": "parameters",
      "text": "<details open><summary><code>NX</code></summary> \n\nSets the value only if `path` has no matches.\n</details>\n\n<details open><summary><code>XX</code></summary> \n\nSets the value only if `path` has one or more matches.\n</details>"
    },
    {
      "id": "examples",
      "title": "Examples",
      "role": "example",
      "text": "<details open>\n<summary><b>Replace an existing value</b></summary>\n\n\nredis> JSON.SET doc $ '{\"a\":2}'\nOK\nredis> JSON.SET doc $.a '3'\nOK\nredis> JSON.GET doc $\n\"[{\\\"a\\\":3}]\"\n\n</details>\n\n<details open>\n<summary><b>Add a new value</b></summary>\n\n\nredis> JSON.SET doc $ '{\"a\":2}'\nOK\nredis> JSON.SET doc $.b '8'\nOK\nredis> JSON.GET doc $\n\"[{\\\"a\\\":2,\\\"b\\\":8}]\"\n\n</details>\n\n<details open>\n<summary><b>Update multiple matches</b></summary>\n\n\nredis> JSON.SET doc $ '{\"f1\": {\"a\":1}, \"f2\":{\"a\":2}}'\nOK\nredis> JSON.SET doc $..a 3\nOK\nredis> JSON.GET doc\n\"{\\\"f1\\\":{\\\"a\\\":3},\\\"f2\\\":{\\\"a\\\":3}}\"\n\n</details>\n\n<details open>\n<summary><b>path does not exist and cannot be created</b></summary>\n\n\nredis> JSON.SET doc $ 1\nOK\nredis> JSON.SET doc $.x.y 2\n(nil)\n\n</details>\n\n<details open>\n<summary><b>XX condition unmet</b></summary>\n\n\nredis> JSON.SET nonexistentkey $ 5 XX\n(nil)\nredis> JSON.GET nonexistentkey\n(nil)\n\n</details>\n\n<details open>\n<summary><b>key does not exist and path is not root</b></summary>\n\n\nredis> JSON.SET nonexistentkey $.x 5\n(error) ERR new objects must be created at the root\n\n</details>"
    },
    {
      "id": "redis-software-and-redis-cloud-compatibility",
      "title": "Redis Software and Redis Cloud compatibility",
      "role": "content",
      "text": "| Redis<br />Software | Redis<br />Cloud | <span style=\"min-width: 9em; display: table-cell\">Notes</span> |\n|:----------------------|:-----------------|:------|\n| <span title=\"Supported\">&#x2705; Supported</span><br /> | <span title=\"Supported\">&#x2705; Flexible & Annual</span><br /><span title=\"Supported\">&#x2705; Free & Fixed</nobr></span> |  |"
    },
    {
      "id": "return-information",
      "title": "Return information",
      "role": "returns",
      "text": "**RESP2:**\n\nOne of the following:\n* [Simple string reply](): `OK` if executed correctly.\n* [Null reply](): if `key` exists but `path` does not exist and cannot be created, or if an `NX` or `XX` condition is unmet.\n* [Simple error reply](): `(error) expected ...` - if the value is invalid.\n* [Simple error reply](): `(error) Error occurred on position ... expected ...` - if the path is invalid.\n* [Simple error reply](): `(error) ERR new objects must be created at the root` - if `key` does not exist and `path` is not root (`$` or `.`).\n* [Simple error reply](): `(error) ERR wrong static path` - if a dynamic path expression has no matching locations.\n* [Simple error reply](): `(error) ERR index out of bounds` - if the path refers to an array index outside the array bounds.\n\n**RESP3:**\n\nOne of the following:\n* [Simple string reply](): `OK` if executed correctly.\n* [Null reply](): if `key` exists but `path` does not exist and cannot be created, or if an `NX` or `XX` condition is unmet.\n* [Simple error reply](): `(error) expected ...` - if the value is invalid.\n* [Simple error reply](): `(error) Error occurred on position ... expected ...` - if the path is invalid.\n* [Simple error reply](): `(error) ERR new objects must be created at the root` - if `key` does not exist and `path` is not root (`$` or `.`).\n* [Simple error reply](): `(error) ERR wrong static path` - if a dynamic path expression has no matching locations.\n* [Simple error reply](): `(error) ERR index out of bounds` - if the path refers to an array index outside the array bounds."
    },
    {
      "id": "see-also",
      "title": "See also",
      "role": "related",
      "text": "[`JSON.GET`]() | [`JSON.MGET`]()"
    },
    {
      "id": "related-topics",
      "title": "Related topics",
      "role": "related",
      "text": "* [RedisJSON]()\n* [Index and search JSON documents]()"
    }
  ],
  "examples": []
}
