JSON.ARRLEN
The JSON.ARRLEN
command in DiceDB is used to retrieve the length of a JSON array at a specified path within a JSON document.
Syntax
Parameters
Parameter | Description | Type | Required |
---|---|---|---|
key | The key under which the JSON document is stored. | String | Yes |
path | The JSONPath to the array within the JSON document. Defaults to root. | String | No |
Return values
Condition | Return Value |
---|---|
JSON array is found at the specified path | Length of the JSON array |
JSONPath contains * wildcard | Indicates the length of each key in the JSON. Returns nil for non-array keys. |
Behaviour
- If the specified
key
does not exist, the command returnsnull
. - If
path
is not specified, the command assumes the root path ($
). - If the specified path exists and points to a JSON array, the command returns the length of the array.
- If the path does not exist or does not point to a JSON array, an error is returned.
- Multiple results are represented as a list in case of wildcards, where each array length is returned in order of appearance.
Errors
-
Wrong number of arguments
:- Error Message:
(error) ERROR wrong number of arguments for 'JSON.ARRLEN' command
- Occurs when the command is executed with less than one argument or with an invalid number of parameters.
- Error Message:
-
Invalid JSONPath
:- Error Message:
(error) ERROR Invalid JSONPath
- Occurs when the specified JSONPath is not valid or incorrect
- Error Message:
-
Path does not exist
:- Error Message:
(error) ERROR Path 'NON_EXISTANT_PATH' does not exist
- Occurs when the provided JSON path does not exist in the document.
- Error Message:
-
Path is not a JSON array
:- Error Message:
(error) ERROR Path 'NON_ARRAY_PATH' does not exist or not array
- Occurs when the specified path does not point to a JSON array.
- Error Message:
Examples Usage
Basic Usage
Setting a key user:1001
with a JSON document:
To get the length of the emails
array:
Root Path
Setting a key user:1002
with a root array:
To get the length of the root array:
Non-Existent Path
Setting a key user:1003
with a JSON document:
To get the length of a non-existent emails
array:
Path is Not an Array
Setting a key user:1004
with a JSON document:
To get the length of a non-array path:
Path with Wildcards
Setting a key user:1005
with a complex JSON document:
To get the length of the language
array using a wildcard path: