JSON.OBJLEN

Since 1.1.0JSON

Syntax

JSON.OBJLEN key [path]
  • Flags: readonly
  • ACL categories: @json, @read, @slow
  • Arity: -2

Returns the number of members in each object path matches in the JSON document at key. With no path it reads the root. Only an object's own members count; the members of objects nested inside it do not.

A JSONPath reports on every value it matches; a legacy path, or no path, on the first match only. See JSON paths. A match that is not an object gives nil for both kinds of path. For the member names themselves, use JSON.OBJKEYS; for arrays and strings, JSON.ARRLEN and JSON.STRLEN.

Reply

  • A JSONPath: an array with one count per match, nil where a match is not an object; an empty array when nothing matches.
  • A legacy path or no path: the count, as an integer; nil when the match is not an object or nothing matches.
  • nil if the key does not exist.

Examples

> JSON.SET d $ '{"name":"Ada","addr":{"city":"London","zip":"N1"},"e":{}}'
OK
> JSON.OBJLEN d
(integer) 3
> JSON.OBJLEN d $.addr
1) (integer) 2
> JSON.OBJLEN d '$.*'
1) (nil)
2) (integer) 2
3) (integer) 0
> JSON.OBJLEN d .name
(nil)