|
1 |
| -Searches for the first occurrence of a scalar JSON value in an array. |
| 1 | +Search for the first occurrence of a scalar JSON value in an array |
2 | 2 |
|
3 |
| -The optional inclusive `start` (default 0) and exclusive `stop` (default 0, meaning that the last element is included) specify a slice of the array to search. |
4 |
| -Negative values are interpreted as starting from the end. |
| 3 | +[Examples](#examples) |
5 | 4 |
|
| 5 | +## Required arguments |
6 | 6 |
|
7 |
| -Note: out-of-range indexes round to the array's start and end. An inverse index range (such as the range from 1 to 0) will return unfound. |
| 7 | +<details open><summary><code>key</code></summary> |
8 | 8 |
|
9 |
| -@return |
| 9 | +is key to parse. |
| 10 | +</details> |
10 | 11 |
|
11 |
| -@array-reply of @integer-reply - the first position in the array of each JSON value that matches the path, -1 if unfound in the array, or @nil-reply if the matching JSON value is not an array. |
| 12 | +<details open><summary><code>value</code></summary> |
12 | 13 |
|
13 |
| -@examples |
| 14 | +is value to find its index in one or more arrays. |
14 | 15 |
|
15 |
| -``` |
16 |
| -redis> JSON.SET doc $ '{"a":[1,2,3,2], "nested": {"a": [3,4]}}' |
17 |
| -OK |
18 |
| -redis> JSON.ARRINDEX doc $..a 2 |
19 |
| -1) (integer) 1 |
20 |
| -2) (integer) -1 |
21 |
| -``` |
| 16 | +{{% alert title="About using strings with JSON commands" color="warning" %}} |
| 17 | +To specify a string as an array value to index, wrap the quoted string with an additional set of single quotes. Example: `'"silver"'`. For more detailed use, see [Examples](#examples). |
| 18 | +{{% /alert %}} |
| 19 | +</details> |
| 20 | + |
| 21 | +## Optional arguments |
| 22 | + |
| 23 | +<details open><summary><code>start</code></summary> |
| 24 | + |
| 25 | +is inclusive start value to specify in a slice of the array to search. Default is `0`. |
| 26 | +</details> |
| 27 | + |
| 28 | + |
| 29 | +<details open><summary><code>stop</code></summary> |
| 30 | + |
| 31 | +is exclusive stop value to specify in a slice of the array to search, including the last element. Default is `0`. Negative values are interpreted as starting from the end. |
| 32 | +</details> |
22 | 33 |
|
23 |
| -``` |
24 |
| -redis> JSON.SET doc $ '{"a":[1,2,3,2], "nested": {"a": false}}' |
| 34 | +{{% alert title="About out-of-range indexes" color="warning" %}} |
| 35 | + |
| 36 | +Out-of-range indexes round to the array's start and end. An inverse index range (such as the range from 1 to 0) returns unfound or `-1`. |
| 37 | +{{% /alert %}} |
| 38 | + |
| 39 | +## Return value |
| 40 | + |
| 41 | +`JSON.ARRINDEX` returns an [array](/docs/reference/protocol-spec/#resp-arrays) of integer replies for each path, the first position in the array of each JSON value that matches the path, `-1` if unfound in the array, or `nil`, if the matching JSON value is not an array. |
| 42 | +For more information about replies, see [Redis serialization protocol specification](/docs/reference/protocol-spec). |
| 43 | + |
| 44 | +## Examples |
| 45 | + |
| 46 | +<details open> |
| 47 | +<summary><b>Find the specific place of a color in a list of product colors</b></summary> |
| 48 | + |
| 49 | +Create a document for noise-cancelling headphones in black and silver colors. |
| 50 | + |
| 51 | +{{< highlight bash >}} |
| 52 | +127.0.0.1:6379> JSON.SET item:1 $ '{"name":"Noise-cancelling Bluetooth headphones","description":"Wireless Bluetooth headphones with noise-cancelling technology","connection":{"wireless":true,"type":"Bluetooth"},"price":99.98,"stock":25,"colors":["black","silver"]}' |
25 | 53 | OK
|
26 |
| -redis> JSON.ARRINDEX doc $..a 2 |
| 54 | +{{< / highlight >}} |
| 55 | + |
| 56 | +Add color `blue` to the end of the `colors` array. `JSON.ARRAPEND` returns the array's new size. |
| 57 | + |
| 58 | +{{< highlight bash >}} |
| 59 | +127.0.0.1:6379> JSON.ARRAPPEND item:1 $.colors '"blue"' |
| 60 | +1) (integer) 3 |
| 61 | +{{< / highlight >}} |
| 62 | + |
| 63 | +Return the new length of the `colors` array. |
| 64 | + |
| 65 | +{{< highlight bash >}} |
| 66 | +JSON.GET item:1 |
| 67 | +"{\"name\":\"Noise-cancelling Bluetooth headphones\",\"description\":\"Wireless Bluetooth headphones with noise-cancelling technology\",\"connection\":{\"wireless\":true,\"type\":\"Bluetooth\"},\"price\":99.98,\"stock\":25,\"colors\":[\"black\",\"silver\",\"blue\"]}" |
| 68 | +{{< / highlight >}} |
| 69 | + |
| 70 | +Get the list of colors for the product. |
| 71 | + |
| 72 | +{{< highlight bash >}} |
| 73 | +127.0.0.1:6379> JSON.GET item:1 '$.colors[*]' |
| 74 | +"[\"black\",\"silver\",\"blue\"]" |
| 75 | +{{< / highlight >}} |
| 76 | + |
| 77 | +Insert two more colors after the second color. You now have five colors. |
| 78 | + |
| 79 | +{{< highlight bash >}} |
| 80 | +127.0.0.1:6379> JSON.ARRINSERT item:1 $.colors 2 '"yellow"' '"gold"' |
| 81 | +1) (integer) 5 |
| 82 | +{{< / highlight >}} |
| 83 | + |
| 84 | +Get the updated list of colors. |
| 85 | + |
| 86 | +{{< highlight bash >}} |
| 87 | +127.0.0.1:6379> JSON.GET item:1 $.colors |
| 88 | +"[[\"black\",\"silver\",\"yellow\",\"gold\",\"blue\"]]" |
| 89 | +{{< / highlight >}} |
| 90 | + |
| 91 | +Find the place where color `silver` is located. |
| 92 | + |
| 93 | +{{< highlight bash >}} |
| 94 | +127.0.0.1:6379> JSON.ARRINDEX item:1 $..colors '"silver"' |
27 | 95 | 1) (integer) 1
|
28 |
| -2) (nil) |
29 |
| -``` |
| 96 | +{{< / highlight >}} |
| 97 | +</details> |
| 98 | + |
| 99 | +## See also |
| 100 | + |
| 101 | +`JSON.ARRAPPEND` | `JSON.ARRINSERT` |
| 102 | + |
| 103 | +## Related topics |
| 104 | + |
| 105 | +* [RedisJSON](/docs/stack/json) |
| 106 | +* [Index and search JSON documents](/docs/stack/search/indexing_json) |
| 107 | + |
0 commit comments