Back to Redis

JSON.CLEAR

content/commands/json.clear.md

latest2.4 KB
Original Source

Clear container values (arrays/objects) and set numeric values to 0

Examples

Required arguments

<details open><summary><code>key</code></summary>

is key to parse.

</details>

Optional arguments

<details open><summary><code>path</code></summary>

is JSONPath to specify. Default is root $. Nonexisting paths are ignored.

</details>

{{% alert title="Note" color="warning" %}}

Already cleared values are ignored for empty containers and zero numbers.

{{% /alert %}}

Examples

<details open> <summary><b>Clear container values and set numeric values to <code>0</code></b></summary>

Create a JSON document.

{{< highlight bash >}} redis> JSON.SET doc $ '{"obj":{"a":1, "b":2}, "arr":[1,2,3], "str": "foo", "bool": true, "int": 42, "float": 3.14}' OK {{< / highlight >}}

Clear all container values. This returns the number of objects with cleared values.

{{< highlight bash >}} redis> JSON.CLEAR doc $.* (integer) 4 {{< / highlight >}}

Get the updated document. Note that numeric values have been set to 0.

{{< highlight bash >}} redis> JSON.GET doc $ "[{"obj":{},"arr":[],"str":"foo","bool":true,"int":0,"float":0}]" {{< / highlight >}}

</details>

Redis Software and Redis Cloud compatibility

| Redis Software | Redis Cloud | <span style="min-width: 9em; display: table-cell">Notes</span> | |:----------------------|:-----------------|:------| | <span title="Supported">✅ Supported</span> | <span title="Supported">✅ Flexible & Annual</span> <span title="Supported">✅ Free & Fixed</nobr></span> | |

Return information

{{< multitabs id="json-clear-return-info" tab1="RESP2" tab2="RESP3" >}}

[Integer reply]({{< relref "/develop/reference/protocol-spec#integers" >}}): the number of matching JSON arrays and objects cleared plus the number of matching JSON numerical values zeroed.

-tab-sep-

[Integer reply]({{< relref "/develop/reference/protocol-spec#integers" >}}): the number of matching JSON arrays and objects cleared plus the number of matching JSON numerical values zeroed.

{{< /multitabs >}}

See also

[JSON.ARRINDEX]({{< relref "commands/json.arrindex/" >}}) | [JSON.ARRINSERT]({{< relref "commands/json.arrinsert/" >}})

  • [RedisJSON]({{< relref "/develop/data-types/json/" >}})
  • [Index and search JSON documents]({{< relref "/develop/ai/search-and-query/indexing/" >}})