MySQL JSON_QUOTE() Function

Summary: in this tutorial, you will learn how to use the MySQL JSON_QUOTE() function to quote a string as a JSON value.

Introduction to the MySQL JSON_QUOTE() function

The JSON_QUOTE() function is used to quote a string as a JSON value by wrapping it with double quote characters and escaping interior quotes and other characters.

Here’s the syntax of the JSON_QUOTE() function:

JSON_QUOTE(string)Code language: SQL (Structured Query Language) (sql)

In this syntax:

  • string: This is the string that you want to wrap as a JSON value.

The function returns a valid JSON value of the input string. It’ll return NUL if the string is NULL.

In practice, you often use the JSON_QUOTE() function to make a valid JSON string literal for inclusion in a JSON document.

MySQL JSON_QUOTE() function examples

Let’s take some examples of using the JSON_QUOTE() function.

1) Quoting a simple string as a JSON value

The following example uses the JSON_QUOTE() function to wrap a simple string:

SELECT JSON_QUOTE('hello');Code language: SQL (Structured Query Language) (sql)

Output:

+---------------------+
| JSON_QUOTE('hello') |
+---------------------+
| "hello"             |
+---------------------+
1 row in set (0.01 sec)Code language: SQL (Structured Query Language) (sql)

2) Quoting a string as a JSON array

SELECT JSON_QUOTE('[1,2,3]');Code language: SQL (Structured Query Language) (sql)

Output:

+-----------------------+
| JSON_QUOTE('[1,2,3]') |
+-----------------------+
| "[1,2,3]"             |
+-----------------------+
1 row in set (0.00 sec)Code language: SQL (Structured Query Language) (sql)

Summary

  • Use the JSON_QUOTE() function to quote a string as a JSON value.
Was this tutorial helpful?