MySQL JSON Functions (original) (raw)

Last Updated : 05 Aug, 2024

**JSON functions in **MySQL allow working with **JSON data in the database and retrieve JSON data from it. These functions help in storing and accessing JSON data efficiently and such data is widely used in web applications as it is **flexible and easy to use.

In this article, We will learn about the **MySQL JSON Functions by understanding various functions in detail along with the examples and so on.

What are MySQL JSON Functions?

Syntax

Here is the basic syntax for some common JSON functions:

**Creating JSON Data:

SELECT JSON_OBJECT('key1', 'value1', 'key2', 'value2');

**Extracting JSON Values:

SELECT JSON_EXTRACT(json_doc, path);

**Modifying JSON Data:

SELECT JSON_SET(json_doc, path, value);

Why Use JSON in MySQL?

Using JSON in MySQL offers several advantages:

1. Flexibility

2. Interoperability

3. Efficiency

4. Scalability

5. Ease of Use

6. Compatibility with NoSQL Features

Examples of MySQL JSON Functions

Example 1: Creating JSON Data

Let's create a JSON object using JSON_OBJECT.

SELECT JSON_OBJECT('name', 'Alice', 'age', 25, 'city', 'Wonderland');

**Output:

{

"name": "Alice",

"age": 25,

"city": "Wonderland"

}

Example 2: Extracting JSON Values

Let's extract a value from a JSON document using JSON_EXTRACT.

SELECT JSON_EXTRACT('{"name": "Alice", "age": 25, "city": "Wonderland"}', '$.age');

**Output:

25

**Explanation: This query extracts the value of the age key from the given JSON document.

Example 3: Modifying JSON Data

Let's modify a JSON document using JSON_SET.

SELECT JSON_SET('{"name": "Alice", "age": 25, "city": "Wonderland"}', '$.age', 26);

**Output:

{

"name": "Alice",

"age": 26,

"city": "Wonderland"

}

**Explanation: This query modifies the value of the age key in the JSON document from 25 to 26.

MySQL JSON Functions Overview

1. JSON_OBJECT

**JSON_OBJECT is the other function which is used to built the JSON object from keys and values in a set. As also mentioned above, this function is especially essential when it comes to the formation of JSON data directly in the SQL statement.

2. JSON_ARRAY

**JSON_ARRAY constructs a JSON array depending on the particular specified values on a list of values. These can used to represent ordered collections of elements.

**Example:

SELECT JSON_ARRAY('Alice', 25, 'Wonderland');

**Output:

["Alice", 25, "Wonderland"]

3. JSON_MERGE

The JSON_MERGE function combines two or more JSON documents into one. This is useful for aggregating data from multiple JSON objects.

**Example:

SELECT JSON_MERGE('{"name": "Alice"}', '{"age": 25}');

**Output:

{"name": "Alice", "age": 25}

4. JSON_REMOVE

The **JSON_REMOVE function deletes a specified key or path from a JSON document.

**Example:

SELECT JSON_REMOVE('{"name": "Alice", "age": 25}', '$.age');

**Output:

{"name": "Alice"}

5. JSON_ARRAY_APPEND

The JSON_ARRAY_APPEND function adds a value to the end of a JSON array.

**Example:

SELECT JSON_ARRAY_APPEND('["Alice", 25]', '$', 'Wonderland');

**Output:

["Alice", 25, "Wonderland"]

The JSON_SEARCH function searches for a value within a JSON document and returns the path to the matching value.

**Example:

SELECT JSON_SEARCH('{"name": "Alice", "city": "Wonderland"}', 'one', 'Wonderland');

**Output:

"$.city"

Conclusion

Overall, **MySQL JSON functions that are available help when one wants to use **JSON within the **MySQL database. These functions are specifically designed to create, extract and modify **JSON documents which simplifies the data structure processes. When properly utilized, these functions allow the improvement of configurability.