Last Updated : 23 Jul, 2025
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?Here is the basic syntax for some common JSON functions:
Creating JSON Data:Extracting JSON Values:SELECT JSON_OBJECT('key1', 'value1', 'key2', 'value2');
Modifying JSON Data:SELECT JSON_EXTRACT(json_doc, path);
Why Use JSON in MySQL?SELECT JSON_SET(json_doc, path, value);
Using JSON in MySQL offers several advantages:
1. FlexibilityLet's create a JSON object using JSON_OBJECT.
SELECT JSON_OBJECT('name', 'Alice', 'age', 25, 'city', 'Wonderland');
Output:
Example 2: Extracting JSON Values{
"name": "Alice",
"age": 25,
"city": "Wonderland"
}
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 DataLet'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_OBJECTJSON_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_ARRAYJSON_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"]
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:
4. JSON_REMOVE{"name": "Alice", "age": 25}
The JSON_REMOVE function deletes a specified key or path from a JSON document.
Example:
SELECT JSON_REMOVE('{"name": "Alice", "age": 25}', '$.age');
Output:
5. JSON_ARRAY_APPEND{"name": "Alice"}
The JSON_ARRAY_APPEND function adds a value to the end of a JSON array.
Example:
SELECT JSON_ARRAY_APPEND('["Alice", 25]', '$', 'Wonderland');
Output:
6. JSON_SEARCH["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:
Conclusion"$.city"
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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4