JSON.ARRTRIM
Trim an array so that it contains only the specified inclusive range of elements
Required arguments
is key to modify.
path
is JSONPath to specify. Default is root $
.
is index of the first element to keep (previous elements are trimmed). Default is 0.
stop
is the index of the last element to keep (following elements are trimmed), including the last element. Default is 0. Negative values are interpreted as starting from the end.
About out-of-range indexes
JSON.ARRTRIM is extremely forgiving, and using it with out-of-range indexes does not produce an error. Note a few differences between how RedisJSON v2.0 and legacy versions handle out-of-range indexes.
Behavior as of RedisJSON v2.0:
- If
start
is < 0, then start from the end of the array. - If
stop
is larger than the end of the array, it is treated like the last element.
Return
Trim an array to a specific set of values
Create two headphone products with maximum sound levels.
dragonfly> JSON.SET key $
OK
Add new sound level values to the second product.
Get the updated array.
dragonfly> JSON.GET key $[1].max_level
Keep only the values between the fifth and the ninth element, inclusive of that last element.
Get the updated array.
dragonfly> JSON.GET key $[1].max_level
"[[140,160,180,200,220]]"