JSON.ARRLEN

Since 1.1.0JSON

Syntax

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

Returns the number of elements in each array path matches in the JSON document at key. With no path it reads the root, which gives a length only when the whole document is an array.

A JSONPath such as $..tags reports on every array it matches; a legacy path such as .tags on the first match only. See JSON paths. JSON.ARRLEN reports nil for a match that is not an array rather than failing, for both kinds of path, as JSON.STRLEN, JSON.OBJLEN and JSON.OBJKEYS do. To check a value's type first, use JSON.TYPE; for the length of strings and objects, JSON.STRLEN and JSON.OBJLEN.

Reply

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

Examples

> JSON.SET d $ '{"tags":["math","code"],"name":"Ada","n":{"tags":[1,2,3]},"e":[]}'
OK
> JSON.ARRLEN d $.tags
1) (integer) 2
> JSON.ARRLEN d .tags
(integer) 2
> JSON.ARRLEN d $..tags
1) (integer) 2
2) (integer) 3
> JSON.ARRLEN d '$.*'
1) (integer) 2
2) (nil)
3) (nil)
4) (integer) 0
> JSON.ARRLEN d .name
(nil)
> JSON.ARRLEN d $.nope
(empty array)