JSON_DEPTH
Description
Returns the maximum depth of a JSON document.
Syntax
JSON_DEPTH(json_doc)
Remarks
The json_doc parameter specifies the JSON document. If the parameter is NULL, NULL is returned.
An error is returned if the parameter is not a valid JSON document.
The depth of an empty array, empty object, or scalar value is 1. The depth of a non-empty array that contains only elements with a depth of 1 is 2. The depth of a non-empty object that contains only members with a value of depth 1 is 2. Otherwise, the depth of the JSON document is greater than 2.
Examples
SELECT JSON_DEPTH('{}'), JSON_DEPTH('[]'), JSON_DEPTH('true');
+------------------+------------------+--------------------+
| JSON_DEPTH('{}') | JSON_DEPTH('[]') | JSON_DEPTH('true') |
+------------------+------------------+--------------------+
| 1 | 1 | 1 |
+------------------+------------------+--------------------+
1 row in set (0.001 sec)
SELECT JSON_DEPTH('[101, 202]'), JSON_DEPTH('[[], {}]');
+--------------------------+------------------------+
| JSON_DEPTH('[101, 202]') | JSON_DEPTH('[[], {}]') |
+--------------------------+------------------------+
| 2 | 2 |
+--------------------------+------------------------+
1 row in set (0.001 sec)
SELECT JSON_DEPTH('[101, {"a": 202}]');
+---------------------------------+
| JSON_DEPTH('[101, {"a": 202}]') |
+---------------------------------+
| 3 |
+---------------------------------+
1 row in set (0.001 sec)
SELECT JSON_DEPTH('{"a":101, "v":"aaa"}');
+------------------------------------+
| JSON_DEPTH('{"a":101, "v":"aaa"}') |
+------------------------------------+
| 2 |
+------------------------------------+
1 row in set (0.001 sec)