Working with JSON in MySQL

Working with JSON in MySQL

JSON (JavaScript Object Notation) is a versatile and human-readable data interchange format. MySQL provides comprehensive support for storing and manipulating JSON data, offering a convenient way to manage JSON data within your relational databases.
Cemil Tokatli
July 12, 2025
Topics:
Share:

JSON support in MySQL allows you to easily handle complex data structures directly in your relational databases. Whether you are dealing with hierarchical data or requiring flexible schemas, JSON integration makes it simpler to manage and query this kind of adaptable data efficiently.

In this article, we'll explore MySQL's JSON features including storage, indexing, and built-in functions that enhance manipulation and querying of JSON data.

JSON Data Type

MySQL supports a JSON data type that allows you to store JSON documents efficiently. This type ensures that all stored JSON values are well-formed at the storage time.

CREATE TABLE user_data (
    id INT AUTO_INCREMENT PRIMARY KEY,
    info JSON
);

By storing JSON documents in MySQL, you can take advantage of MySQL's optimization capabilities for storing and querying JSON data.

Inserting JSON Data

When inserting JSON data into your tables, ensure that JSON strings are properly formatted.

INSERT INTO user_data (info) VALUES ('{"name": "John Doe", "age": 30, "city": "New York"}');

If you attempt to insert malformed JSON data, MySQL will raise an error.

Retrieving JSON Data

To retrieve JSON data, use the column name directly. You can also access specific JSON keys using the -> operator or the ->> operator for JSON retrieval.

SELECT info FROM user_data;

SELECT info->'$.name' AS name FROM user_data;  -- Retrieves the "name" key from JSON
SELECT info->>'$.name' AS name FROM user_data; -- Same, but returns the data as text

Modifying JSON Data

The JSON_SET function can be used to update values within a JSON document, allowing you to modify existing keys or add new ones.

UPDATE user_data SET info = JSON_SET(info, '$.age', 31) WHERE id = 1;

The above command updates the age key to 31 for the JSON data where id is 1.

Working with JSON Arrays

JSON arrays offer a flexible way to store multiple values within a single JSON object. Here's how you can work with arrays in JSON using MySQL:

  • Inserting an Array: You can insert JSON arrays directly into a JSON column.

    INSERT INTO user_data (info) VALUES ('{"name": "Jane Doe", "hobbies": ["reading", "traveling", "swimming"]}');
    
  • Reading an Array Value at a Given Index: Use the JSON path to access a specific element in an array.

    SELECT info->'$.hobbies[0]' AS first_hobby FROM user_data WHERE id = 2;
    
  • Getting the Length of the Array: The JSON_LENGTH function returns the number of elements in a JSON array.

    SELECT JSON_LENGTH(info->'$.hobbies') AS num_hobbies FROM user_data WHERE id = 2;
    
  • Removing an Item from an Array: The JSON_REMOVE function allows you to delete an element at a specific index.

    UPDATE user_data SET info = JSON_REMOVE(info, '$.hobbies[1]') WHERE id = 2;
    
  • Updating an Item in an Array: Utilize the JSON_SET function to modify an element at a particular index.

    UPDATE user_data SET info = JSON_SET(info, '$.hobbies[1]', 'cycling') WHERE id = 2;
    
  • Inserting an Item into an Array: While JSON arrays don't support direct insertion at an index, you can construct a new array by concatenating elements.

    UPDATE user_data SET info = JSON_SET(info, '$.hobbies', JSON_ARRAY_APPEND(info->'$.hobbies', '$', 'new_hobby')) WHERE id = 2;
    

These operations allow you to manipulate JSON arrays efficiently within your MySQL tables.

JSON Functions

MySQL offers a wide variety of functions to manipulate and query JSON data efficiently:

1. JSON_OBJECT()

Creates JSON objects from key-value pairs.

SELECT JSON_OBJECT('name', 'Alice', 'age', 28);

Result:

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

2. JSON_ARRAY()

Creates JSON arrays.

SELECT JSON_ARRAY(1, 'two', 3, 'four');

Result:

[1, "two", 3, "four"]

3. JSON_MERGE_PRESERVE()

Merges two or more JSON documents, preserving duplicate keys by creating arrays.

SELECT JSON_MERGE_PRESERVE('{"name": "John"}', '{"city": "New York"}');

Result:

{"name": "John", "city": "New York"}

4. JSON_UNQUOTE()

Removes quotes from JSON values.

SELECT JSON_UNQUOTE(JSON_EXTRACT('{"city": "New York"}', '$.city'));

Result:

New York

5. JSON_EXTRACT()

Extracts data from a JSON document based on a given path.

SELECT JSON_EXTRACT('{"name": "John", "age": 30}', '$.age');

Result:

30

6. JSON_REMOVE()

Removes data from a JSON document.

SELECT JSON_REMOVE('{"name": "John", "age": 30}', '$.age');

Result:

{"name": "John"}

7. JSON_CONTAINS()

Checks if a JSON document contains another JSON value.

SELECT JSON_CONTAINS('{"name": "John", "age": 30}', '{"age": 30}');

Result:

1

8. JSON_CONTAINS_PATH()

Checks whether a specific path exists within a JSON document.

SELECT JSON_CONTAINS_PATH('{"name": "John", "age": 30}', 'one', '$.age');

Result:

1

9. JSON_OVERLAPS()

Checks if any key-value pairs from two JSON documents overlap.

SELECT JSON_OVERLAPS('{"name": "John", "age": 30}', '{"age": 30, "name": "Jane"}');

Result:

1

10. JSON_TABLE()

The JSON_TABLE() function allows you to create a relational view over JSON data, offering a structured format that can be queried just like a regular table. This is particularly useful for scenarios where you need to interact with JSON data in a tabular format for reporting or integration purposes.

SELECT *
FROM JSON_TABLE(
  '[{"name": "John", "age": 30}, {"name": "Jane", "age": 25}]',
  '$[*]' COLUMNS (
    name VARCHAR(100) PATH '$.name',
    age INT PATH '$.age'
  )
) AS jt;

Result:

name age
John 30
Jane 25

Imagine a scenario where you have a JSON document containing detailed customer order information, including nested arrays and objects. Here's how JSON_TABLE can be used to flatten and retrieve specific details:

SELECT *
FROM JSON_TABLE(
  '[
    {"orderId": 1, "customer": {"name": "John Doe", "email": "john@example.com"}, "items": [{"productName": "Laptop", "quantity": 1}, {"productName": "Mouse", "quantity": 2}]},
    {"orderId": 2, "customer": {"name": "Jane Smith", "email": "jane@example.com"}, "items": [{"productName": "Keyboard", "quantity": 1}]}
  ]',
  '$[*]' COLUMNS (
    orderId INT PATH '$.orderId',
    customerName VARCHAR(100) PATH '$.customer.name',
    customerEmail VARCHAR(100) PATH '$.customer.email',
    productName VARCHAR(100) PATH '$.items[0].productName',
    productQuantity INT PATH '$.items[0].quantity'
  )
) AS orders;

Result:

orderId customerName customerEmail productName productQuantity
1 John Doe john@example.com Laptop 1
2 Jane Smith jane@example.com Keyboard 1

In a real-world application, you would typically store this JSON data in a database table and then utilize JSON_TABLE to extract and work with the data in a relational format. Consider the following steps to demonstrate this:

Creating a Table and Inserting JSON Data:

First, create a table to store JSON data:

CREATE TABLE orders_json (
    id INT AUTO_INCREMENT PRIMARY KEY,
    order_data JSON
);

INSERT INTO orders_json (order_data) VALUES
('{"orderId": 1, "customer": {"name": "John Doe", "email": "john@example.com"}, "items": [{"productName": "Laptop", "quantity": 1}, {"productName": "Mouse", "quantity": 2}]}'),
('{"orderId": 2, "customer": {"name": "Jane Smith", "email": "jane@example.com"}, "items": [{"productName": "Keyboard", "quantity": 1}]}');

Querying with JSON_TABLE:

Now, use JSON_TABLE to flatten this JSON data for detailed analysis:

SELECT o.id,
    jt.orderId,
    jt.customerName,
    jt.customerEmail,
    jt.productName,
    jt.productQuantity
FROM orders_json o,
  JSON_TABLE(
    o.order_data,
    '$' COLUMNS (
      orderId INT PATH '$.orderId',
      customerName VARCHAR(100) PATH '$.customer.name',
      customerEmail VARCHAR(100) PATH '$.customer.email',
      productName VARCHAR(100) PATH '$.items[0].productName',
      productQuantity INT PATH '$.items[0].quantity'
    )
  ) AS jt;

This query retrieves detailed information from each JSON document in the orders_json table, presenting it in a flat relational structure, which is optimal for analysis and reporting. By integrating JSON_TABLE with existing table structures, you can easily work with complex JSON data in a relational format, thus enhancing the capability to perform meaningful data manipulation and extraction in real-world applications.

Conclusion

MySQL's robust support for JSON provides a powerful method for handling flexible data, allowing you to store, query, and manipulate JSON efficiently. With a variety of functions and features, MySQL facilitates the management of complex data structures and enhances data manipulation capabilities. By leveraging these JSON features, you can take advantage of powerful data storage techniques while maintaining the flexibility of JSON structures within your relational databases.