Skip to main content
Version: V1.0.0

JSON_LENGTH

Description

This function returns the length of the JSON document. If the path parameter is specified, it returns the length of the value identified by the path.

Syntax

JSON_LENGTH(json_doc[, path])

Remarks

The json_doc parameter specifies the JSON document, and path specifies the path. If any parameter is NULL or the path parameter does not identify a value in the document, the function returns NULL.

The following situations will result in an error:

  • The json_doc parameter is not a valid JSON document.

  • The path parameter is not a valid path expression.

  • The path parameter contains the * or ** wildcard.

The length of a JSON document is defined as follows:

  • The length of a scalar is 1.

  • The length of an array is the number of elements in the array.

  • The length of an object is the number of members in the object.

  • The length of nested arrays or objects is not counted.

Examples

SELECT JSON_LENGTH('[1, 2, {"a": 3}, 4]');
+------------------------------------+
| JSON_LENGTH('[1, 2, {"a": 3}, 4]') |
+------------------------------------+
| 4 |
+------------------------------------+
1 row in set (0.001 sec)

SELECT JSON_LENGTH('{"a": 1, "b": {"c": 303}}');
+------------------------------------------+
| JSON_LENGTH('{"a": 1, "b": {"c": 303}}') |
+------------------------------------------+
| 2 |
+------------------------------------------+
1 row in set (0.001 sec)

SELECT JSON_LENGTH('{"a": 1, "b": {"c": 30}}', '$.b');
+------------------------------------------------+
| JSON_LENGTH('{"a": 1, "b": {"c": 30}}', '$.b') |
+------------------------------------------------+
| 1 |
+------------------------------------------------+
1 row in set (0.001 sec)