JSON_QUOTE()
This page is part of MariaDB's Documentation.
The parent of this page is: Functions for MariaDB Xpand
Topics on this page:
Overview
Returns a double-quoted JSON string from a string argument.
USAGE
JSON_QUOTE(string)
Argument Name | Description |
---|---|
| The string to transform |
DETAILS
JSON_QUOTE()
is a JSON function that transforms a string value into a JSON string and returns the result.
A JSON string is enclosed in double quotes and has certain characters escaped with a preceding backslash, such as double quotes and backslashes.
A NULL
is returned if the string
argument is NULL
.
EXAMPLES
With Literal Values
In the following example, JSON_QUOTE()
is called with a string literal value that contains special characters. The result demonstrates how to quote and escape values for JSON:
SET @str = 'Now "is" the \\ time';
SELECT @str, JSON_QUOTE(@str)\G
*************************** 1. row ***************************
@str: Now "is" the \ time
JSON_QUOTE(@str): "Now \"is\" the \\ time"
SELECT JSON_QUOTE(5) AS result;
+--------+
| result |
+--------+
| "5" |
+--------+
SELECT JSON_QUOTE(NULL) AS result;
+--------+
| result |
+--------+
| NULL |
+--------+
Example Schema and Data
Some of the examples are based on the inventory
table:
CREATE TABLE inventory (
item VARCHAR(20),
category TEXT,
count INT
) CHARSET utf8mb4;
INSERT INTO inventory VALUES
('Foo', '"in stock"', 42), ('Foo', 'back order', 50),
('Bar', '"in stock"', 10), ('Bar', 'back order', 60),
('Baz', '"in stock"', 99), ('Baz', 'back order', 10);
Simple Query
JSON_QUOTE()
encloses all values in double quotes and escapes any special characters:
SELECT JSON_QUOTE(item) AS item,
JSON_QUOTE(category) AS category
FROM inventory
WHERE category!='back order' AND count>5\G
*************************** 1. row ***************************
item: "Foo"
category: "\"in stock\""
*************************** 2. row ***************************
item: "Bar"
category: "\"in stock\""
*************************** 3. row ***************************
item: "Baz"
category: "\"in stock\""