{"id":18427,"date":"2021-07-10T10:01:39","date_gmt":"2021-07-10T00:01:39","guid":{"rendered":"https:\/\/database.guide\/?p=18427"},"modified":"2021-07-10T10:01:39","modified_gmt":"2021-07-10T00:01:39","slug":"mariadb-json_quote-explained","status":"publish","type":"post","link":"https:\/\/database.guide\/mariadb-json_quote-explained\/","title":{"rendered":"MariaDB JSON_QUOTE() Explained"},"content":{"rendered":"\n<p>In MariaDB, <code>JSON_QUOTE()<\/code> is a built-in function that produces a valid JSON string literal that can be included in a JSON document.&nbsp;<\/p>\n\n\n\n<p>It wraps the string with double quote characters and escapes interior quotes and other special characters, returning a utf8mb4 string.<\/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_QUOTE(json_value)<\/code><\/pre>\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_QUOTE('Small') AS 'Result';<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+---------+\n| Result  |\n+---------+\n| \"Small\" |\n+---------+<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Escape Characters<\/h2>\n\n\n\n<p>In addition to wrapping the string in double quotes, <code>JSON_QUOTE()<\/code> also escapes interior quotes and other special characters.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT JSON_QUOTE('By \"small\", she meant \"tiny\"') AS 'Result';<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+------------------------------------+\n| Result                             |\n+------------------------------------+\n| \"By \\\"small\\\", she meant \\\"tiny\\\"\" |\n+------------------------------------+<\/pre>\n\n\n\n<p>In this case, the original string contains double quotes and so the resulting string escapes each double quote with a backslash. <\/p>\n\n\n\n<p>If the double quotes weren&#8217;t escaped, they would interfere with the outer double quotes, and inadvertently terminate the string early. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">JSON Objects<\/h2>\n\n\n\n<p>Here&#8217;s an example of quoting a string representation of a JSON object:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT JSON_QUOTE('{ \"name\": \"Brandy\"}');<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+-----------------------------------+\n| JSON_QUOTE('{ \"name\": \"Brandy\"}') |\n+-----------------------------------+\n| \"{ \\\"name\\\": \\\"Brandy\\\"}\"         |\n+-----------------------------------+<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Arrays<\/h2>\n\n\n\n<p>Here&#8217;s an example of quoting a string representation of an array:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT JSON_QUOTE('&#91; \"Small\", \"Medium\", \"Large\" ]');<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+----------------------------------------------+\n| JSON_QUOTE('[ \"Small\", \"Medium\", \"Large\" ]') |\n+----------------------------------------------+\n| \"[ \\\"Small\\\", \\\"Medium\\\", \\\"Large\\\" ]\"       |\n+----------------------------------------------+<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Numbers<\/h2>\n\n\n\n<p>Passing a number returns <code>NULL<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT JSON_QUOTE(10) AS 'Result';<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+--------+\n| Result |\n+--------+\n| NULL   |\n+--------+<\/pre>\n\n\n\n<p>The number would need to be provided as a string. It would then be returned as a quoted string.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT JSON_QUOTE('10') AS 'Result';<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+--------+\n| Result |\n+--------+\n| \"10\"   |\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_QUOTE(null);<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+------------------+\n| JSON_QUOTE(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_QUOTE()<\/code> without an argument results in an error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT JSON_QUOTE();<\/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_QUOTE'<\/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_QUOTE('a', 'b');<\/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_QUOTE'<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">More Examples<\/h2>\n\n\n\n<p>MariaDB <code>JSON_QUOTE()<\/code> works the same as the MySQL function of the same name. See <a href=\"https:\/\/database.guide\/json_quote-how-to-escape-characters-in-strings-used-as-json-values-in-mysql\/\"><code>JSON_QUOTE()<\/code> &#8211; How to Escape Characters in Strings used as JSON Values in MySQL<\/a> for more examples.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In MariaDB, JSON_QUOTE() is a built-in function that produces a valid JSON string literal that can be included in a JSON document.&nbsp; It wraps the string with double quote characters and escapes interior quotes and other special characters, returning a utf8mb4 string.<\/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-18427","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\/18427","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=18427"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/18427\/revisions"}],"predecessor-version":[{"id":18538,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/18427\/revisions\/18538"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=18427"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=18427"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=18427"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}