{"id":18319,"date":"2021-07-07T08:26:05","date_gmt":"2021-07-06T22:26:05","guid":{"rendered":"https:\/\/database.guide\/?p=18319"},"modified":"2021-07-07T08:26:05","modified_gmt":"2021-07-06T22:26:05","slug":"mariadb-json_depth-explained","status":"publish","type":"post","link":"https:\/\/database.guide\/mariadb-json_depth-explained\/","title":{"rendered":"MariaDB JSON_DEPTH() Explained"},"content":{"rendered":"\n<p>In MariaDB, <code>JSON_DEPTH()<\/code> is a built-in function that allows you to check the depth of a JSON document.<\/p>\n\n\n\n<p>It accepts the JSON document as an argument, and returns the maximum depth of the document.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<p>The syntax goes like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>JSON_DEPTH(json_doc)<\/code><\/pre>\n\n\n\n<p>Where <code>json_doc<\/code> is the JSON document for which to return the depth.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Here&#8217;s an example to demonstrate.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT JSON_DEPTH('{ \"name\": \"Wag\" }');<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+---------------------------------+\n| JSON_DEPTH('{ \"name\": \"Wag\" }') |\n+---------------------------------+\n|                               2 |\n+---------------------------------+<\/pre>\n\n\n\n<p>In this case, the depth is <code>2<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Scalar Values &amp; Empty Objects\/Arrays<\/h2>\n\n\n\n<p>Scalar values or empty arrays or objects have a depth of <code>1<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    JSON_DEPTH('{}'),\n    JSON_DEPTH('&#91;]'),\n    JSON_DEPTH(1);<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+------------------+------------------+---------------+\n| JSON_DEPTH('{}') | JSON_DEPTH('[]') | JSON_DEPTH(1) |\n+------------------+------------------+---------------+\n|                1 |                1 |             1 |\n+------------------+------------------+---------------+<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Deeper JSON Document<\/h2>\n\n\n\n<p>Here&#8217;s an example that uses a JSON document with a depth of <code>4<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET @json_document = '\n    { \n        \"_id\" : 1, \n        \"name\" : \"Wag\", \n        \"details\" : {\n            \"type\" : \"Dog\", \n            \"weight\" : 20,\n            \"awards\" : { \n                \"Florida Dog Awards\" : \"Top Dog\", \n                \"New York Marathon\" : \"Fastest Dog\", \n                \"Sumo 2020\" : \"Biggest Dog\"\n            }\n        }\n    }\n';\nSELECT JSON_DEPTH(@json_document);<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+----------------------------+\n| JSON_DEPTH(@json_document) |\n+----------------------------+\n|                          4 |\n+----------------------------+<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Null Arguments<\/h2>\n\n\n\n<p>If the argument is <code>NULL<\/code>, the result is <code>NULL<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT JSON_DEPTH(null);<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+------------------+\n| JSON_DEPTH(null) |\n+------------------+\n|             NULL |\n+------------------+<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Invalid JSON<\/h2>\n\n\n\n<p>Passing invalid JSON results in <code>NULL<\/code> with a warning:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT JSON_DEPTH('{1}');<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+-------------------+\n| JSON_DEPTH('{1}') |\n+-------------------+\n|              NULL |\n+-------------------+\n1 row in set, 1 warning (0.000 sec)<\/pre>\n\n\n\n<p>Let&#8217;s see the warning:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SHOW WARNINGS;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+---------+------+--------------------------------------------------------------------------------+\n| Level   | Code | Message                                                                        |\n+---------+------+--------------------------------------------------------------------------------+\n| Warning | 4038 | Syntax error in JSON text in argument 1 to function 'json_depth' at position 2 |\n+---------+------+--------------------------------------------------------------------------------+<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Incorrect Parameter Count<\/h2>\n\n\n\n<p>Providing no arguments results in an error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT JSON_DEPTH();<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_DEPTH'<\/pre>\n\n\n\n<p>It&#8217;s the same when you provide too many arguments:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT JSON_DEPTH('{\"a\": 1}', 2);<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_DEPTH'<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In MariaDB, JSON_DEPTH() is a built-in function that allows you to check the depth of a JSON document. It accepts the JSON document as an argument, and returns the maximum depth of the document.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[117],"tags":[93,80,20],"class_list":["post-18319","post","type-post","status-publish","format-standard","hentry","category-mariadb","tag-functions","tag-json","tag-what-is"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/18319","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=18319"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/18319\/revisions"}],"predecessor-version":[{"id":18325,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/18319\/revisions\/18325"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=18319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=18319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=18319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}