MariaDB JSON_QUOTE() Function
In MariaDB, JSON_QUOTE()
is a built-in function that wraps a value in double quotes, making it a JSON string value.
MariaDB JSON_QUOTE()
Syntax
Here is the syntax for the MariaDB JSON_QUOTE()
function:
JSON_QUOTE(str)
Parameters
str
-
Required. a string.
If you supply the wrong number of arguments, MariaDB will report an error: ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_QUOTE'
.
Return value
The MariaDB JSON_QUOTE()
function returns a JSON string value surrounded by double quotes.
If the argument is NULL
, the JSON_QUOTE()
function returns NULL
.
Special characters in the following table will be escaped with a backslash:
Escape sequence | Sequence of characters |
---|---|
\" |
Double quotes " |
\b |
backspace character |
\f |
Form feed |
\n |
line break |
\r |
carriage return |
\t |
Tabs |
\\ |
backslash \ |
\uXXXX |
UTF-8 bytes of the Unicode value XXXX |
MariaDB JSON_QUOTE()
Examples
Here are some common examples to show the usages of the Mariadb JSON_QUOTE()
function.
Basic example
SELECT
JSON_QUOTE('123'),
JSON_QUOTE('NULL'),
JSON_QUOTE('"NULL"');
Output:
+-------------------+--------------------+----------------------+
| JSON_QUOTE('123') | JSON_QUOTE('NULL') | JSON_QUOTE('"NULL"') |
+-------------------+--------------------+----------------------+
| "123" | "NULL" | "\"NULL\"" |
+-------------------+--------------------+----------------------+
Escape character
In addition to enclosing strings in double quotes, JSON_QUOTE()
escape inner quotes and other special characters.
SELECT JSON_QUOTE('I am "strong"');
Output:
+-----------------------------+
| JSON_QUOTE('I am "strong"') |
+-----------------------------+
| "I am \"strong\"" |
+-----------------------------+
Numbers
If the argument is a number, JSON_QUOTE()
will return NULL
:
SELECT JSON_QUOTE(123);
Output:
+-----------------+
| JSON_QUOTE(123) |
+-----------------+
| NULL |
+-----------------+
NULL
parameter
If the argument is NULL
, JSON_QUOTE()
will return NULL
:
SELECT JSON_QUOTE(NULL);
Output:
+------------------+
| JSON_QUOTE(NULL) |
+------------------+
| NULL |
+------------------+
Conclusion
In MariaDB, JSON_QUOTE()
is a built-in function that wraps a value in double quotes, making it a JSON string value.