JSON_DEPTH
Discover JSON_DEPTH in MariaDB. This function returns the maximum depth of a JSON document, assigning a depth of 1 to scalars and empty structures, and higher values for nested data.
Syntax
JSON_DEPTH(json_doc)Description
Returns the maximum depth of the given JSON document, or NULL if the argument is null. An error occurs if the argument is an invalid JSON document.
Scalar values or empty arrays or objects have a depth of 1.
Arrays with only scalar values and objects with only scalar values for all keys have depth of 1.
In all other cases, the depth can be 2 or greater.
There is no maximum depth level — it's unlimited.
For more information, see this blog post.
The maximum depth is 32.
Examples
SELECT JSON_DEPTH('[]'), JSON_DEPTH('true'), JSON_DEPTH('{}');
+------------------+--------------------+------------------+
| JSON_DEPTH('[]') | JSON_DEPTH('true') | JSON_DEPTH('{}') |
+------------------+--------------------+------------------+
| 1 | 1 | 1 |
+------------------+--------------------+------------------+
SELECT JSON_DEPTH('[1, 2, 3]'), JSON_DEPTH('[[], {}, []]');
+-------------------------+----------------------------+
| JSON_DEPTH('[1, 2, 3]') | JSON_DEPTH('[[], {}, []]') |
+-------------------------+----------------------------+
| 2 | 2 |
+-------------------------+----------------------------+
SELECT JSON_DEPTH('[1, 2, [3, 4, 5, 6], 7]');
+---------------------------------------+
| JSON_DEPTH('[1, 2, [3, 4, 5, 6], 7]') |
+---------------------------------------+
| 3 |
+---------------------------------------+This page is licensed: CC BY-SA / Gnu FDL
Last updated
Was this helpful?

