{"id":18326,"date":"2021-07-07T08:25:36","date_gmt":"2021-07-06T22:25:36","guid":{"rendered":"https:\/\/database.guide\/?p=18326"},"modified":"2021-07-07T08:25:36","modified_gmt":"2021-07-06T22:25:36","slug":"mariadb-json_exists-explained","status":"publish","type":"post","link":"https:\/\/database.guide\/mariadb-json_exists-explained\/","title":{"rendered":"MariaDB JSON_EXISTS() Explained"},"content":{"rendered":"\n<p>In MariaDB, <code>JSON_EXISTS()<\/code> is a built-in function that allows you to check whether a value exists at a specified path in the JSON document.<\/p>\n\n\n\n<p>It accepts the JSON document as an argument, and returns&nbsp;<code>1<\/code>&nbsp;if the path is found, and&nbsp;<code>0<\/code>&nbsp;if it&#8217;s not.<\/p>\n\n\n\n<p>The <a rel=\"noreferrer noopener\" href=\"https:\/\/mariadb.com\/kb\/en\/json_exists\/\" data-type=\"URL\" data-id=\"https:\/\/mariadb.com\/kb\/en\/json_exists\/\" target=\"_blank\">MariaDB documentation<\/a> states that the function &#8220;determines whether a specified JSON value exists in the given data&#8221;. However, the function doesn&#8217;t actually appear to check for a given value. It&#8217;s probably more accurate to say that it determines whether a specified <em>path<\/em> exists, or that a value exists at the specified path.<\/p>\n\n\n\n<p>For checking that the actual <em>value<\/em> exists, you can use the <code><a href=\"https:\/\/database.guide\/mariadb-json_contains-explained\/\" data-type=\"post\" data-id=\"18296\">JSON_CONTAINS()<\/a><\/code> function.<\/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_EXISTS(json_doc, path)<\/code><\/pre>\n\n\n\n<p>Where <code>json_doc<\/code> is the JSON document and <code>path<\/code> is the path to find.<\/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_EXISTS('{ \"name\": \"Wag\" }', '$.name');<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+--------------------------------------------+\n| JSON_EXISTS('{ \"name\": \"Wag\" }', '$.name') |\n+--------------------------------------------+\n|                                          1 |\n+--------------------------------------------+<\/pre>\n\n\n\n<p>In this case, the path is found and the result is <code>1<\/code>.<\/p>\n\n\n\n<p>If the path isn&#8217;t found, the result is <code>0<\/code>, like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT JSON_EXISTS('{ \"name\": \"Wag\" }', '$.type');<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+--------------------------------------------+\n| JSON_EXISTS('{ \"name\": \"Wag\" }', '$.type') |\n+--------------------------------------------+\n|                                          0 |\n+--------------------------------------------+<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Arrays<\/h2>\n\n\n\n<p>In this example, I check for the existence of an element at a given index of an array:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT JSON_EXISTS(\n    '{ \n        \"name\": \"Wag\", \n        \"scores\": &#91; 10, 8, 7 ] \n    }', \n    \"$.scores&#91;2]\"\n    ) AS Result;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+--------+\n| Result |\n+--------+\n|      1 |\n+--------+<\/pre>\n\n\n\n<p>Here&#8217;s what happens if the index doesn&#8217;t exist in the array:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT JSON_EXISTS(\n    '{ \n        \"name\": \"Wag\", \n        \"scores\": &#91; 10, 8, 7 ] \n    }', \n    '$.scores&#91;3]'\n    ) AS Result;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+--------+\n| Result |\n+--------+\n|      0 |\n+--------+<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Larger JSON Document<\/h2>\n\n\n\n<p>Here&#8217;s an example that uses a slightly larger JSON document. Here, I check for a key that exists within several levels of nesting:<\/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_EXISTS(\n    @json_document, \n    '$.details.awards.Sumo 2020'\n    ) AS Result;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+--------+\n| Result |\n+--------+\n|      1 |\n+--------+<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Null Arguments<\/h2>\n\n\n\n<p>If any of the arguments are <code>NULL<\/code>, the result is <code>NULL<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    JSON_EXISTS(null, '$.a'),\n    JSON_EXISTS('{ \"a\": 1 }', null);<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+--------------------------+---------------------------------+\n| JSON_EXISTS(null, '$.a') | JSON_EXISTS('{ \"a\": 1 }', null) |\n+--------------------------+---------------------------------+\n|                     NULL |                            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>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT JSON_EXISTS('{1}', '$.a');<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+---------------------------+\n| JSON_EXISTS('{1}', '$.a') |\n+---------------------------+\n|                      NULL |\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_EXISTS();<\/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_EXISTS'<\/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_EXISTS('{\"a\": 1}', '$.a', 3);<\/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_EXISTS'<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In MariaDB, JSON_EXISTS() is a built-in function that allows you to check whether a value exists at a specified path in the JSON document. It accepts the JSON document as an argument, and returns&nbsp;1&nbsp;if the path is found, and&nbsp;0&nbsp;if it&#8217;s not. The MariaDB documentation states that the function &#8220;determines whether a specified JSON value exists &#8230; <a title=\"MariaDB JSON_EXISTS() Explained\" class=\"read-more\" href=\"https:\/\/database.guide\/mariadb-json_exists-explained\/\" aria-label=\"Read more about MariaDB JSON_EXISTS() Explained\">Read more<\/a><\/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-18326","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\/18326","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=18326"}],"version-history":[{"count":7,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/18326\/revisions"}],"predecessor-version":[{"id":18450,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/18326\/revisions\/18450"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=18326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=18326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=18326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}