JSON.ARRINSERT
The JSON.ARRINSERT
command allows you to insert one or more JSON values into an array at a specified path before a given index. All existing elements in the array are shifted to the right to make room for the new elements.
This command returns an array of integer replies for each path, where each integer represents the new size of the array after the insertion. If the path does not exist or is not an array, it returns an error.
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 |
index | Position where values will be inserted. Positive for start-to-end, negative for end-to-start. | Integer | Yes |
value | JSON value(s) to be inserted into the array. | JSON | Yes |
Return Values
Condition | Return Value |
---|---|
Array successfully updated | (Int) Integer representing the new array size at each matched path |
Key does not exist | Error: (error) ERR key does not exist |
Path is not valid | Error: (error) ERR Existing key has wrong Dice type |
Invalid JSON value | Error: (error) ERR Path <path> does not exist |
Invalid index | Error: (error) ERR index out of bounds |
Behaviour
- The command inserts specified JSON values into the array located at the provided path within the document stored under the given key.
- Values are inserted before the specified index, shifting existing elements to the right.
- If the index is out of bounds, it is adjusted to the nearest valid position within the array.
- Errors are returned if the path does not point to an array or if the index provided is not a valid integer.
Errors
-
Wrong number of arguments
:- Error Message:
(error) ERR wrong number of arguments for JSON.ARRINSERT command
- Raised if the number of arguments are less or more than expected.
- Error Message:
-
Key doesn't exist
:- Error Message:
(error) ERR key does not exist
- Raised if the specified key does not exist in the DiceDB database.
- Error Message:
-
Key has wrong Dice type
:- Error Message:
(error) ERR Existing key has wrong Dice type
- Raised if thevalue of the specified key doesn’t match the specified value in DIceDb
- Error Message:
-
Path doesn't exist
:- Error Message:
(error) ERR Path <path> does not exist
- Raised if any of the provided JSON values are not valid JSON.
- Error Message:
-
failed to do the operation
:- Error Message:
(error) ERR failed to modify JSON data
- Raised if the argument specifying path contains an invalide path.
- Error Message:
Example Usage
Basic Usage
Inserting at a valid index in the root path
Using negative index
Inserting at a negative index
Having nested arrays as value
Handling nested arrays
Using out of bounds index
Inserting with an out-of-bounds index
Invalid index type
Invalid index type