{"id":18237,"date":"2021-07-02T06:27:53","date_gmt":"2021-07-01T20:27:53","guid":{"rendered":"https:\/\/database.guide\/?p=18237"},"modified":"2021-07-03T09:23:18","modified_gmt":"2021-07-02T23:23:18","slug":"mariadb-json_object-explained","status":"publish","type":"post","link":"https:\/\/database.guide\/mariadb-json_object-explained\/","title":{"rendered":"MariaDB JSON_OBJECT() Explained"},"content":{"rendered":"\n<p>In MariaDB, <code>JSON_OBJECT()<\/code> is a built-in function that returns a JSON object containing the key\/value pairs provided as arguments.<\/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_OBJECT(&#91;key, value&#91;, key, value] ...])<\/code><\/pre>\n\n\n\n<p>The function accepts any number of key\/value pairs. <\/p>\n\n\n\n<p>The key\/value list can also be empty.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Here&#8217;s a simple example to demonstrate the function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT JSON_OBJECT(\"name\", \"Homer\", \"type\", \"Idiot\");<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+-----------------------------------------------+\n| JSON_OBJECT(\"name\", \"Homer\", \"type\", \"Idiot\") |\n+-----------------------------------------------+\n| {\"name\": \"Homer\", \"type\": \"Idiot\"}            |\n+-----------------------------------------------+<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">No Arguments<\/h2>\n\n\n\n<p>As mentionend, the argument list can be empty, in which case, an empty object will be returned:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT JSON_OBJECT();<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+---------------+\n| JSON_OBJECT() |\n+---------------+\n| {}            |\n+---------------+<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Incorrect Parameter Count<\/h2>\n\n\n\n<p>However, if you do provide arguments, they must be an even number of arguments (so that each key has a corresponding value). <\/p>\n\n\n\n<p>Here&#8217;s what happens when I pass just one argument to <code>JSON_OBJECT()<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT JSON_OBJECT(\"name\");<\/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_OBJECT'<\/pre>\n\n\n\n<p>Same error occurs if I pass three arguments:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT JSON_OBJECT(\"name\", \"Homer\", \"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_OBJECT'<\/pre>\n\n\n\n<p>And so on&#8230;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Null Key Names<\/h2>\n\n\n\n<p>The <a href=\"https:\/\/mariadb.com\/kb\/en\/json_object\/\" data-type=\"URL\" data-id=\"https:\/\/mariadb.com\/kb\/en\/json_object\/\" target=\"_blank\" rel=\"noreferrer noopener\">MariaDB documentation<\/a> states that an error is returned if any key name is <code>NULL<\/code>. <\/p>\n\n\n\n<p>However, I get a different result:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT JSON_OBJECT(null, null);<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+-------------------------+\n| JSON_OBJECT(null, null) |\n+-------------------------+\n| {\"\": null}              |\n+-------------------------+<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In MariaDB, JSON_OBJECT() is a built-in function that returns a JSON object containing the key\/value pairs provided as arguments.<\/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-18237","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\/18237","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=18237"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/18237\/revisions"}],"predecessor-version":[{"id":18243,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/18237\/revisions\/18243"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=18237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=18237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=18237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}