Skip to content

JSON_QUOTE()

Function Description

The JSON_QUOTE function is used to convert a string value to a string in JSON format. By wrapping the string with double quotes and escaping the inner quotes and other characters, quote the string as a JSON value, and then return the result as a utf8mb4 string. If the parameter is NULL, NULL is returned.

The JSON_QUOTE function is usually used to generate valid JSON strings to be included in a JSON document.

Grammar Structure

select JSON_QUOTE(string_value);

Particle description

string_value is a string to be converted to a JSON string. This function returns a JSON format string where the original string has been surrounded by quotes and properly escaped.

Example

mysql> SELECT JSON_QUOTE('null'), JSON_QUOTE('"null"');
+------------------------------------------------------------------------------------------------------------------------------
| json_quote(null) | json_quote("null") |
+------------------------------------------------------------------------------------------------------------------------------
| "null" | "\"null\"" |
+------------------------------------------------------------------------------------------------------------------------------
1 row in set (0.00 sec)
mysql> SELECT JSON_QUOTE('[1, 2, 3]');
+---------------------------+
| json_quote([1, 2, 3]) |
+---------------------------+
| "[1, 2, 3]" |
+---------------------------+
1 row in set (0.00 sec)

mysql> SELECT JSON_QUOTE('hello world');
+------------------------------+
| json_quote(hello world) |
+------------------------------+
| "hello world" |
+------------------------------+
1 row in set (0.00 sec)

As you can see, the original string is surrounded by quotes and the double quotes in the string are also escaped. This way, it can be used as a value in JSON format, for example, as a property value of a JSON object.