JSON.ARRTRIM

Usage:
JSON.ARRTRIM key path start end
Complexity:
O(N) where N is the number of json arrays matched by the path.
Module:
valkey-json
Since module version:
1.0.0
ACL Categories:
@write, @fast, @json

Trim arrays at the path so that it becomes subarray [start, end], both inclusive.

  • If the array is empty, do nothing, return 0.
  • If start < 0, treat it as 0.
  • If end >= size (size of the array), treat it as size-1.
  • If start >= size or start > end, empty the array and return 0.

Examples

Enhanced path syntax:

127.0.0.1:6379> JSON.SET k1 . '[[], ["a"], ["a", "b"], ["a", "b", "c"]]'
OK
127.0.0.1:6379> JSON.ARRTRIM k1 $[*] 0 1
1) (integer) 0
2) (integer) 1
3) (integer) 2
4) (integer) 2
   127.0.0.1:6379> JSON.GET k1
   "[[],[\"a\"],[\"a\",\"b\"],[\"a\",\"b\"]]"

Restricted path syntax:

127.0.0.1:6379> JSON.SET k1 . '{"children": ["John", "Jack", "Tom", "Bob", "Mike"]}'
OK
127.0.0.1:6379> JSON.ARRTRIM k1 .children 0 1
(integer) 2
127.0.0.1:6379> JSON.GET k1 .children
"[\"John\",\"Jack\"]"

RESP2 Reply

  • If the path is enhanced syntax:

    • Array reply: Array of integers representing the new length of the array at each path.

    • Nil reply: For each path where the value is an empty array or not an array.

  • If the path is restricted syntax:

  • Simple error reply:

    • if the value at the path is not an array (only for restricted syntax).

    • if an index argument is out of bounds.

RESP3 Reply

  • If the path is enhanced syntax:

    • Array reply: Array of integers representing the new length of the array at each path.

    • Null reply: For each path where the value is an empty array or not an array.

  • If the path is restricted syntax:

  • Simple error reply:

    • if the value at the path is not an array (only for restricted syntax).

    • if an index argument is out of bounds.