Skip to main content
Returns the length of a JSON document. If a path is specified, this function returns the length of the value identified by the path. Tip All of the JSON functions and operators are listed in the navigation and on the overview page Accelerate your queries with generated columns The length of a document is determined according to the following rules:
  • The length of a scalar value is 1. For example, the length of 1, "a", true, false, and null is 1.
  • The length of an array is the number of array elements. For example, the length of [1, 2] is 2.
  • The length of an object is the number of object members. For example, the length of {"a": 1} is 1.
  • The length of nested arrays or objects does not count. For example, the length of {"a": [1, 2]} is 1, because the nested array [1, 2] is not calculated into length.

Syntax

Parameters

json_doc: required, the JSON document for which to return the length. path: optional. It is used to return the length of a value within the document. The path generally starts with $ and uses . as the path separator. [] is used as the array subscript, which starts from 0.

Return value

Returns a value of the INT type. An error is returned if the JSON document is not a valid document. 0 is returned in any of the following scenarios:
  • The path does not identify a value in the document.
  • The path is not a valid path expression.
  • The path contains the * or ** wildcard.

Examples

Example 1: Return the length of a scalar value.
Example 2: Return the length of an empty object.
Example 3: Return the length of an object that has data.
Example 4: Return the length of a JSON array.
text
Example 5: Return the length of a JSON array in which one element has a nested array. The nested array [3, 4] is not calculated into length.
text
Example 6: Return the length of an object specified by path $.Person.
Example 7: Return the length of the value specified by path $.y.
text