JSON.ARRINDEX
The JSON.ARRINDEX command in DiceDB provides users with the ability to search for the position of a specific element within a JSON array stored at a specified path in a document identified by a given key. By executing this command, users can efficiently locate the index of an element that matches the provided value, enabling streamlined data access and manipulation.
This functionality is especially useful for developers dealing with large or nested JSON arrays who need to pinpoint the location of particular elements for further processing or validation. With support for specifying paths and flexible querying, JSON.ARRINDEX enhances the capability of managing and navigating complex JSON datasets within DiceDB.
Syntax
Parameters
Parameter | Description | Type | Required |
---|---|---|---|
key | The name of the key holding the JSON document. | String | Yes |
path | JSONPath pointing to an array within the JSON document. | String | Yes |
value | The value to search for within the array in JSON document. | Mixed | Yes |
start | Optional index to start the search from. Defaults to 0. | Integer | No |
stop | Optional index to end the search. Defaults to 0. | Integer | No |
Return Values
Condition | Return Value |
---|---|
Success | ([]integer) array of integer replies for each path, the first position in the array of each JSON value that matches the path |
Wrong number of arguments | Error: (error) ERR wrong number of arguments for JSON.ARRINDEX command |
Key has wrong type | Error: (error) ERR Existing key has wrong Dice type |
Invalid integer arguments | Error: (error) ERR Couldn't parse as integer |
Invalid jsonpath | Error: (error) ERR Path '<path>' does not exist |
Behaviour
When the JSON.ARRINDEX command is issued, DiceDB performs the following steps:
- It checks if argument count is valid or not. If not, an error is thrown.
- It checks for the validity of value, start(optional) and stop(optional) argument passed. If not, an error is thrown.
- If the jsonpath passed is invalid, an error is thrown.
- It checks the type of value passed corresponding to the key. If it is not appropriate, error is thrown.
- For each value matching the path, it checks if the value is JSON array.
- If it is JSON array, it finds the first occurrence of the value.
- If value is found, it adds to array the index where the value was found. Else, -1 is added.
- If it is not JSON array, (nil) is added to resultant array.
- The final array is returned.
Errors
-
Wrong number of arguments
:- Error Message:
(error) ERR wrong number of arguments for JSON.ARRINDEX command
- Raised if the number of arguments are less or more than expected.
- Error Message:
-
Couldn't parse as integer
:- Error Message:
(error) ERR Couldn't parse as integer
- Raised if the optional start and stop argument are non-integer strings.
- Raised if the value is not a valid integer.
- Error Message:
-
Key has wrong Dice type
:- Error Message:
(error) ERR Existing key has wrong Dice type
- Raised if the value of the specified key doesn’t match the specified value in DiceDb
- Error Message:
-
Path '<path>' does not exist
- Error Message:
(error) ERR Path '<path>' does not exist
- Raise if the path passed is not valid.
- Error Message:
Example Usage
Basic usage
Searches for the first occurrence of a JSON value in an array
Finding the occurrence of value starting from given index
Searches for the first occurrence of a JSON value in an array starting from given index
Finding the occurrence of value starting from given index and ending at given index (exclusive)
Searches for the first occurrence of a JSON value in [start, stop) range
When invalid start and stop argument is passed
Error When invalid start and stop argument is passed
When the jsonpath is not array object
Error When jsonpath is not array object
When the jsonpath is not valid path
Error When jsonpath is not valid path