What's new in Valkey 9.0? Discover new features and improvements. Read the announcement.

JSON.OBJLEN

Get the number of keys in the object at the specified path.

Usage
JSON.OBJLEN key [path]
Complexity
O(N) where N is the number of json objects matched by the path.
Module
valkey-json
Since module version
1.0.0
ACL Categories
@read, @fast, @json

Get number of keys in the object values at the path.

Examples

Enhanced path syntax:

127.0.0.1:6379> JSON.SET k1 $ '{"a":{}, "b":{"a":"a"}, "c":{"a":"a", "b":"bb"}, "d":{"a":1, "b":"b", "c":{"a":3,"b":4}}, "e":1}'
OK
127.0.0.1:6379> JSON.OBJLEN k1 $.a
1) (integer) 0
127.0.0.1:6379> JSON.OBJLEN k1 $.a.*
(empty array)
127.0.0.1:6379> JSON.OBJLEN k1 $.b
1) (integer) 1
127.0.0.1:6379> JSON.OBJLEN k1 $.b.*
1) (nil)
127.0.0.1:6379> JSON.OBJLEN k1 $.c
1) (integer) 2
127.0.0.1:6379> JSON.OBJLEN k1 $.c.*
1) (nil)
2) (nil)
127.0.0.1:6379> JSON.OBJLEN k1 $.d
1) (integer) 3
127.0.0.1:6379> JSON.OBJLEN k1 $.d.*
1) (nil)
2) (nil)
3) (integer) 2
127.0.0.1:6379> JSON.OBJLEN k1 $.*
1) (integer) 0
2) (integer) 1
3) (integer) 2
4) (integer) 3
5) (nil)

Restricted path syntax:

127.0.0.1:6379> JSON.SET k1 . '{"a":{}, "b":{"a":"a"}, "c":{"a":"a", "b":"bb"}, "d":{"a":1, "b":"b", "c":{"a":3,"b":4}}, "e":1}'
OK
127.0.0.1:6379> JSON.OBJLEN k1 .a
(integer) 0
127.0.0.1:6379> JSON.OBJLEN k1 .a.*
(error) NONEXISTENT JSON path does not exist
127.0.0.1:6379> JSON.OBJLEN k1 .b
(integer) 1
127.0.0.1:6379> JSON.OBJLEN k1 .b.*
(error) WRONGTYPE JSON element is not an object
127.0.0.1:6379> JSON.OBJLEN k1 .c
(integer) 2
127.0.0.1:6379> JSON.OBJLEN k1 .c.*
(error) WRONGTYPE JSON element is not an object
127.0.0.1:6379> JSON.OBJLEN k1 .d
(integer) 3
127.0.0.1:6379> JSON.OBJLEN k1 .d.*
(integer) 2
127.0.0.1:6379> JSON.OBJLEN k1 .*
(integer) 0

Replies

RESP2

  • If the path is enhanced syntax:
    • Array reply: Array of integers representing the object length at each path.
    • Nil reply: For each path where the value is not an object.
    • Nil reply: If the document key does not exist.
  • If the path is restricted syntax:
    • Integer reply: Number of keys in the object.
    • Integer reply: If multiple objects are selected, returns the first object's length.
    • Nil reply: If the document key does not exist.
  • Simple error reply:
    • if the value at the path is not an object (only for restricted syntax).
    • if the path does not exist (only for restricted syntax).

RESP3

  • If the path is enhanced syntax:
    • Array reply: Array of integers representing the object length at each path.
    • Null reply: For each path where the value is not an object.
    • Null reply: If the document key does not exist.
  • If the path is restricted syntax:
  • Simple error reply:
    • if the value at the path is not an object (only for restricted syntax).
    • if the path does not exist (only for restricted syntax).