JSON.ARRTRIM
The JSON.ARRTRIM
command in DiceDB modifies a JSON array by removing elements outside a specified range, effectively trimming it to contain only desired elements. This in-place operation is part of the DiceDBJSON module, which enables seamless handling and manipulation of JSON data 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 |
index | The index of the first element to retain in the array (0-based index). | Integer | Yes |
value | The index of the last element to retain in the array (inclusive). | Integer | Yes |
Return Values
Condition | Return Value |
---|---|
Success | (Int) Integer representing the length of the array after the append operation. |
Key does not exist | Error: (error) ERR key does not exist |
Wrong number of arguments | Error: (error) ERR wrong number of arguments for JSON.ARRTRIM command |
Key has wrong type | Error: (error) ERR Existing key has wrong Dice type |
Path doesn’t exist | Error: (error) ERR Path <path> does not exist |
Operation failed | Error: (error) ERR failed to modify JSON data |
Behaviour
- The command trims the JSON array located at the specified path within the document stored under the given key to only include elements within the specified start and stop indices.
- Elements outside of the specified range are removed from the array, modifying it in place.
- If the start or stop indices are out of bounds, they are adjusted to the nearest valid index within the array.
- An error is returned if the path does not point to an array or if the key does not exist in the DiceDB database.
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
Trimming an array to a specific range
Trim array down to 1 element
Trimming an array to a single element
Trimming array with out of bound index
Trimming an array with out-of-bounds indices
When path doesn’t exist
Error when path does not exist
When key doesn’t exist
Error when key does not exist