JSON_QUOTE()

Overview

Returns a double-quoted JSON string from a string argument.

USAGE

JSON_QUOTE(string)

Argument Name

Description

string

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.

SYNONYMS

SCHEMA

PARAMETERS

SKYSQL

PRIVILEGES

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\""

ERROR HANDLING

FEATURE INTERACTION

RESPONSES

DIAGNOSIS

ISO 9075:2016

CHANGE HISTORY

Release Series

History

23.09

  • Present starting in MariaDB Xpand 23.09.1.

6.1

  • Present starting in MariaDB Xpand 6.1.0.

6.0

  • Present starting in MariaDB Xpand 6.0.3.

5.3

  • Present starting in MariaDB Xpand 5.3.13.

Release Series

History

6.0

  • Present starting in MariaDB Xpand 6.0.3.

5.3

  • Present starting in MariaDB Xpand 5.3.13.

Release Series

History

6.1

  • Present starting in MariaDB Xpand 6.1.0.

EXTERNAL REFERENCES