{"id":18296,"date":"2021-07-03T14:45:41","date_gmt":"2021-07-03T04:45:41","guid":{"rendered":"https:\/\/database.guide\/?p=18296"},"modified":"2021-07-03T14:45:41","modified_gmt":"2021-07-03T04:45:41","slug":"mariadb-json_contains-explained","status":"publish","type":"post","link":"https:\/\/database.guide\/mariadb-json_contains-explained\/","title":{"rendered":"MariaDB JSON_CONTAINS() Explained"},"content":{"rendered":"\n<p>In MariaDB, <code>JSON_CONTAINS()<\/code> is a built-in function that allows you to find out whether a specified value is found in the given JSON document, or at a specific path within the document. <\/p>\n\n\n\n<p>It returns&nbsp;<code>1<\/code>&nbsp;if it does contain the value,&nbsp;<code>0<\/code>&nbsp;if it doesn&#8217;t, and <code>NULL<\/code> if any of the arguments are <code>NULL<\/code>.<\/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_CONTAINS(json_doc, val&#91;, path])<\/code><\/pre>\n\n\n\n<p>Where <code>json_doc<\/code> is the JSON document, <code>val<\/code> is the value to find, and <code>path<\/code> an optional value that specifies a path within the document.<\/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>SET @json_document = '{ \"name\": \"Wag\", \"weight\": 10 }';\n\nSELECT JSON_CONTAINS(@json_document, '{\"name\": \"Wag\"}');<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+--------------------------------------------------+\n| JSON_CONTAINS(@json_document, '{\"name\": \"Wag\"}') |\n+--------------------------------------------------+\n|                                                1 |\n+--------------------------------------------------+<\/pre>\n\n\n\n<p>In this case, there was a match, and the result is <code>1<\/code>.<\/p>\n\n\n\n<p>In the next example, there&#8217;s no match:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET @json_document = '{ \"name\": \"Wag\", \"weight\": 10 }';\n\nSELECT JSON_CONTAINS(@json_document, '{\"name\": \"Woof\"}');<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+---------------------------------------------------+\n| JSON_CONTAINS(@json_document, '{\"name\": \"Woof\"}') |\n+---------------------------------------------------+\n|                                                 0 |\n+---------------------------------------------------+<\/pre>\n\n\n\n<p>Note that the value is enclosed in curly braces. <\/p>\n\n\n\n<p>Here&#8217;s what happens when the second argument is not valid:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET @json_document = '{ \"name\": \"Wag\", \"weight\": 10 }';\n\nSELECT JSON_CONTAINS(@json_document, 'Wag');<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+--------------------------------------+\n| JSON_CONTAINS(@json_document, 'Wag') |\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 2 to function 'json_contains' at position 1 |\n+---------+------+-----------------------------------------------------------------------------------+<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Specify a Path<\/h2>\n\n\n\n<p>You can optionally use a third argument to specify a path.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET @json_document = '{ \"name\": \"Wag\", \"weight\": 10 }';\n\nSELECT JSON_CONTAINS(@json_document, 10, '$.weight');<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+-----------------------------------------------+\n| JSON_CONTAINS(@json_document, 10, '$.weight') |\n+-----------------------------------------------+\n|                                             1 |\n+-----------------------------------------------+<\/pre>\n\n\n\n<p>When specifying a path, I didn&#8217;t need to use curly braces.<\/p>\n\n\n\n<p>Here&#8217;s one searching for a string:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET @json_document = '{ \"name\": \"Wag\", \"weight\": 10 }';\n\nSELECT JSON_CONTAINS(@json_document, '\"Wag\"', '$.name');<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+--------------------------------------------------+\n| JSON_CONTAINS(@json_document, '\"Wag\"', '$.name') |\n+--------------------------------------------------+\n|                                                1 |\n+--------------------------------------------------+<\/pre>\n\n\n\n<p>Notice I used double quotes inside the single quotes. If I omit the double quotes, here&#8217;s what happens:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET @json_document = '{ \"name\": \"Wag\", \"weight\": 10 }';\n\nSELECT JSON_CONTAINS(@json_document, 'Wag', '$.name');<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+------------------------------------------------+\n| JSON_CONTAINS(@json_document, 'Wag', '$.name') |\n+------------------------------------------------+\n|                                           NULL |\n+------------------------------------------------+\n1 row in set, 1 warning (0.000 sec)<\/pre>\n\n\n\n<p>And let&#8217;s check 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 2 to function 'json_contains' at position 1 |\n+---------+------+-----------------------------------------------------------------------------------+<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Nested Structures<\/h2>\n\n\n\n<p>Here&#8217;s an example that looks for a value within a nested document:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET @json_document = '{ \"name\": \"Wag\", \"specs\": { \"weight\": 10, \"height\": 30 } }';\n\nSELECT JSON_CONTAINS(@json_document, 30, '$.specs.height');<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+-----------------------------------------------------+\n| JSON_CONTAINS(@json_document, 30, '$.specs.height') |\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>SET @json_document = '{ \"name\": \"Wag\", \"weight\": 10 }';\nSELECT \n    JSON_CONTAINS(null, 10, '$.weight') AS a,\n    JSON_CONTAINS(@json_document, null, '$.weight') AS b,\n    JSON_CONTAINS(@json_document, 10, null) AS c;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+------+------+------+\n| a    | b    | c    |\n+------+------+------+\n| NULL | NULL | 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_CONTAINS();<\/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_CONTAINS'<\/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_CONTAINS('{ \"a\": 1}', 1, 2, 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_CONTAINS'<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In MariaDB, JSON_CONTAINS() is a built-in function that allows you to find out whether a specified value is found in the given JSON document, or at a specific path within the document. It returns&nbsp;1&nbsp;if it does contain the value,&nbsp;0&nbsp;if it doesn&#8217;t, and NULL if any of the arguments are NULL.<\/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-18296","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\/18296","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=18296"}],"version-history":[{"count":6,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/18296\/revisions"}],"predecessor-version":[{"id":18328,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/18296\/revisions\/18328"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=18296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=18296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=18296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}