top of page
CerebroSQL

MySQL: 

JSON_LENGTH

Syntax:
JSON_LENGTH(json_doc[, path])

Returns the length of a JSON document, or, if a path argument is given,
the length of the value within the document identified by the path.
Returns NULL if any argument is NULL or the path argument does not
identify a value in the document. An error occurs if the json_doc
argument is not a valid JSON document or the path argument is not a
valid path expression or contains a * or ** wildcard.

The length of a document is determined as follows:

o The length of a scalar is 1.

o The length of an array is the number of array elements.

o The length of an object is the number of object members.

o The length does not count the length of nested arrays or objects.

URL: https://dev.mysql.com/doc/refman/8.0/en/json-attribute-functions.html

Example

mysql> SELECT JSON_LENGTH('[1, 2, {"a": 3}]');
+---------------------------------+
| JSON_LENGTH('[1, 2, {"a": 3}]') |
+---------------------------------+
| 3 |
+---------------------------------+
mysql> SELECT JSON_LENGTH('{"a": 1, "b": {"c": 30}}');
+-----------------------------------------+
| JSON_LENGTH('{"a": 1, "b": {"c": 30}}') |
+-----------------------------------------+
| 2 |
+-----------------------------------------+
mysql> SELECT JSON_LENGTH('{"a": 1, "b": {"c": 30}}', '$.b');
+------------------------------------------------+
| JSON_LENGTH('{"a": 1, "b": {"c": 30}}', '$.b') |
+------------------------------------------------+
| 1 |
+------------------------------------------------+

bottom of page