{"id":18486,"date":"2021-07-13T08:03:15","date_gmt":"2021-07-12T22:03:15","guid":{"rendered":"https:\/\/database.guide\/?p=18486"},"modified":"2021-07-13T08:03:15","modified_gmt":"2021-07-12T22:03:15","slug":"mariadb-json_type-explained","status":"publish","type":"post","link":"https:\/\/database.guide\/mariadb-json_type-explained\/","title":{"rendered":"MariaDB JSON_TYPE() Explained"},"content":{"rendered":"\n<p>In MariaDB, <code>JSON_TYPE()<\/code> is a built-in function that returns the type of a JSON value, as a string.&nbsp;<\/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_TYPE(json_val)<\/code><\/pre>\n\n\n\n<p>Where <code>json_val<\/code> is the value for which to return the type.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Object Types<\/h2>\n\n\n\n<p>The possible return types are listed in the following table:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Return type<\/th><th>Value<\/th><th>Example<\/th><\/tr><tr><td><code>ARRAY<\/code><\/td><td>JSON array.<\/td><td><code>[1, 2, 3]<\/code><\/td><\/tr><tr><td><code>OBJECT<\/code><\/td><td>JSON object.<\/td><td><code>{\"a\":\"1\"}<\/code><\/td><\/tr><tr><td><code>BOOLEAN<\/code><\/td><td>JSON true\/false literal.<\/td><td><code>true<\/code> or <code>false<\/code><\/td><\/tr><tr><td><code>DOUBLE<\/code><\/td><td>A number with at least one floating point decimal.<\/td><td><code>1.2<\/code><\/td><\/tr><tr><td><code>INTEGER<\/code><\/td><td>A number without a floating point decimal.<\/td><td><code>1<\/code><\/td><\/tr><tr><td><code>NULL<\/code><\/td><td>JSON <code>null<\/code> literal. This is returned as a string, and is not to be confused with the SQL <code>NULL<\/code> value.<\/td><td><code>null<\/code><\/td><\/tr><tr><td><code>STRING<\/code><\/td><td>JSON string.<\/td><td><code>\"bird\"<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\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 = '\n    { \n        \"name\" : \"Fluffy\", \n        \"type\" : \"Cat\" \n    }\n';\n\nSELECT JSON_TYPE(@json);<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+------------------+\n| JSON_TYPE(@json) |\n+------------------+\n| OBJECT           |\n+------------------+<\/pre>\n\n\n\n<p>In this example I passed a whole JSON document, which has a type of <code>OBJECT<\/code>.<\/p>\n\n\n\n<p>Here are more examples:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    JSON_TYPE('&#91;1, 2, 3]'),\n    JSON_TYPE('{ \"a\" : 1 }'),\n    JSON_TYPE('true'),\n    JSON_TYPE('false'),\n    JSON_TYPE(10.59),\n    JSON_TYPE(10),\n    JSON_TYPE(null),\n    JSON_TYPE('\"Fuzzy Smith\"');<\/code><\/pre>\n\n\n\n<p>Result (using vertical output):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">    JSON_TYPE('[1, 2, 3]'): ARRAY\n  JSON_TYPE('{ \"a\" : 1 }'): OBJECT\n         JSON_TYPE('true'): BOOLEAN\n        JSON_TYPE('false'): BOOLEAN\n          JSON_TYPE(10.59): DOUBLE\n             JSON_TYPE(10): INTEGER\n           JSON_TYPE(null): NULL\nJSON_TYPE('\"Fuzzy Smith\"'): STRING<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Extract the Value from a JSON Document<\/h2>\n\n\n\n<p>In the previous examples, I passed each value directly to the function as a literal. <\/p>\n\n\n\n<p>We can combine <code>JSON_TYPE()<\/code> with other functions, such as <code><a href=\"https:\/\/database.guide\/mariadb-json_extract-explained\/\" data-type=\"post\" data-id=\"18342\">JSON_EXTRACT()<\/a><\/code> to find out the type of a value within a larger JSON document. Here&#8217;s an example of extracting a value from a JSON document in order to find out its type:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET @json = '\n    { \n        \"name\" : \"Wag\", \n        \"scores\" : &#91;8, 0, 9] \n    }\n';\n\nSELECT \n    JSON_TYPE(JSON_EXTRACT(@json, '$.name')) AS Result;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+--------+\n| Result |\n+--------+\n| STRING |\n+--------+<\/pre>\n\n\n\n<p>Here&#8217;s another example that returns more types:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET @json = '\n    { \n        \"name\" : \"Wag\", \n        \"scores\" : &#91;8, 0, 9],\n        \"weight\" : 10.50,\n        \"height\" : null,\n        \"age\" : 4\n    }\n';\n\nSELECT \n    JSON_TYPE(\n        JSON_EXTRACT(@json, '$.name')\n        ) AS a,\n    JSON_TYPE(\n        JSON_EXTRACT(@json, '$.scores')\n        ) AS b,\n    JSON_TYPE(\n        JSON_EXTRACT(@json, '$.scores&#91;0]')\n        ) AS c,\n    JSON_TYPE(\n        JSON_EXTRACT(@json, '$.weight')\n        ) AS d,\n    JSON_TYPE(\n        JSON_EXTRACT(@json, '$.height')\n        ) AS e,\n    JSON_TYPE(\n        JSON_EXTRACT(@json, '$.age')\n        ) AS f;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+--------+-------+---------+--------+------+---------+\n| a      | b     | c       | d      | e    | f       |\n+--------+-------+---------+--------+------+---------+\n| STRING | ARRAY | INTEGER | DOUBLE | NULL | INTEGER |\n+--------+-------+---------+--------+------+---------+<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Null Argument<\/h2>\n\n\n\n<p>If the argument is <code>null<\/code>, then <code>null<\/code> is returned.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT JSON_TYPE(null);<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+-----------------+\n| JSON_TYPE(null) |\n+-----------------+\n| NULL            |\n+-----------------+<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Incorrect Parameter Count<\/h2>\n\n\n\n<p>Calling <code>JSON_TYPE()<\/code> without an argument results in an error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT JSON_TYPE();<\/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_TYPE'<\/pre>\n\n\n\n<p>It&#8217;s the same when too many arguments are passed:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT JSON_TYPE(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_TYPE'<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In MariaDB, JSON_TYPE() is a built-in function that returns the type of a JSON value, as a string.&nbsp;<\/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-18486","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\/18486","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=18486"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/18486\/revisions"}],"predecessor-version":[{"id":18677,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/18486\/revisions\/18677"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=18486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=18486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=18486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}