MySQL tutorial: JSON_DEPTH [EN]
top of page
CerebroSQL

MySQL: 

JSON_DEPTH

Syntax:
JSON_DEPTH(json_doc)

Returns the maximum depth of a JSON document. Returns NULL if the
argument is NULL. An error occurs if the argument is not a valid JSON
document.

An empty array, empty object, or scalar value has depth 1. A nonempty
array containing only elements of depth 1 or nonempty object containing
only member values of depth 1 has depth 2. Otherwise, a JSON document
has depth greater than 2.

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

Example

mysql> SELECT JSON_DEPTH('{}'), JSON_DEPTH('[]'), JSON_DEPTH('true');
+------------------+------------------+--------------------+
| JSON_DEPTH('{}') | JSON_DEPTH('[]') | JSON_DEPTH('true') |
+------------------+------------------+--------------------+
| 1 | 1 | 1 |
+------------------+------------------+--------------------+
mysql> SELECT JSON_DEPTH('[10, 20]'), JSON_DEPTH('[[], {}]');
+------------------------+------------------------+
| JSON_DEPTH('[10, 20]') | JSON_DEPTH('[[], {}]') |
+------------------------+------------------------+
| 2 | 2 |
+------------------------+------------------------+
mysql> SELECT JSON_DEPTH('[10, {"a": 20}]');
+-------------------------------+
| JSON_DEPTH('[10, {"a": 20}]') |
+-------------------------------+
| 3 |
+-------------------------------+

bottom of page