JSON.STRLEN

Since 1.1.0JSON

Syntax

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

Returns the length of each string path matches in the JSON document at key. The length is in bytes of the string's UTF-8 text, without quotes, so "Zürich" has length 7: ü takes two bytes.

A JSONPath reports on every value it matches; a legacy path, or no path, on the first match only. See JSON paths. For a JSONPath, a match that is not a string gives nil. For a legacy path, one that matches nothing is ERR Path does not exist, and a value that is not a string WRONGTYPE wrong type of path value - expected string, as in RedisJSON, since 1.2.1; 1.2.0 answered nil for both. To add to a string, use JSON.STRAPPEND, which replies with the new length in the same unit.

Reply

  • A JSONPath: an array with one length per match, nil where a match is not a string; an empty array when nothing matches.
  • A legacy path or no path: the length, as an integer.
  • ERR Path does not exist, or WRONGTYPE wrong type of path value - expected string, for a legacy path.
  • nil if the key does not exist.

Examples

> JSON.SET d $ '{"name":"Ada L.","city":"Zürich","age":36,"kids":[{"name":"Al"},{"name":"Bea"}]}'
OK
> JSON.STRLEN d $.name
1) (integer) 6
> JSON.STRLEN d .city
(integer) 7
> JSON.STRLEN d $..name
1) (integer) 6
2) (integer) 2
3) (integer) 3
> JSON.STRLEN d $.age
1) (nil)