MySQL Storing and Querying JSON Data

By Raman Kumar

Updated on Nov 11, 2024

In this tutorial, we'll explain MySQL storing and querying JSON data.

JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write and easy for machines to parse and generate. MySQL introduced JSON data types in version 5.7.8, allowing for storage and querying of JSON data directly within the database. This capability is increasingly valuable as JSON has become a standard format for transmitting structured data in web applications.

We’ll cover how to use MySQL’s JSON data types effectively, with examples on storing, retrieving, and querying JSON data.

MySQL Storing and Querying JSON Data

1. What Is the JSON Data Type in MySQL?

The JSON data type in MySQL allows you to store JSON-formatted strings as JSON data in a structured and optimized way. JSON values are validated and stored in an optimized binary format, making it faster and more efficient to read and write.

MySQL’s JSON data type supports the following:

  • Nested structures
  • Efficient storage with binary encoding
  • JSON-specific functions for retrieval and modification

2. Creating Tables with JSON Data Type

To create a table with a JSON column, define the column using the JSON keyword.

Example:

CREATE TABLE employees (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50),
    details JSON
);

In this example:

The details column is of type JSON, and it can store any JSON-formatted data, such as nested objects or arrays.

3. Inserting JSON Data

Inserting JSON data into a JSON column is similar to inserting strings, but you must ensure the JSON is valid.

Example:

INSERT INTO employees (name, details) VALUES 
('Alice', '{ "age": 30, "position": "Developer", "skills": ["JavaScript", "MySQL"] }'),
('Bob', '{ "age": 40, "position": "Manager", "skills": ["Leadership", "Communication"] }');

In this example:

  • JSON data includes information like age, position, and skills.
  • Ensure the JSON data is properly formatted with double quotes around keys and string values.

4. Querying JSON Data

Retrieving JSON Data

To retrieve JSON data, use SELECT as usual. MySQL provides functions to access specific elements within JSON data.

Example:

SELECT name, details->'$.age' AS age FROM employees;

In this example:

  • The -> operator extracts the value at a specified path.
  • The $ symbol represents the root of the JSON document.

Filtering JSON Data

You can filter results based on JSON data with the -> or ->> operators or by using JSON functions.

Example:

SELECT * FROM employees WHERE details->'$.position' = 'Developer';

In this example:

Only records where the position attribute in details is Developer will be retrieved.

5. Modifying JSON Data

Updating JSON Data

You can update JSON values using JSON_SET, JSON_INSERT, and JSON_REPLACE.

  • JSON_SET: Updates a value at a given path, adding a new attribute if it doesn’t exist.
  • JSON_INSERT: Inserts a new value but does not overwrite existing ones.
  • JSON_REPLACE: Updates a value only if it exists.

Example:

UPDATE employees 
SET details = JSON_SET(details, '$.position', 'Senior Developer') 
WHERE name = 'Alice';

This command updates Alice’s position to “Senior Developer.”

Adding New Attributes

To add new information, such as a new skill, use JSON_ARRAY_APPEND.

Example:

UPDATE employees 
SET details = JSON_ARRAY_APPEND(details, '$.skills', 'Python') 
WHERE name = 'Alice';

In this example:

The skills array for Alice is updated to include “Python.”

6. Using JSON Functions

MySQL includes a range of JSON functions that allow you to work effectively with JSON data. Some commonly used functions are:

  • JSON_EXTRACT: Extracts a value at a specified path.
  • JSON_KEYS: Returns the keys of a JSON object.
  • JSON_LENGTH: Returns the length of a JSON document (array or object).
  • JSON_TYPE: Returns the type of a JSON value (e.g., object, array, string).
  • JSON_CONTAINS: Checks if a JSON document contains a specific value.


1. JSON_EXTRACT - Extracts a Value at a Specified Path

The JSON_EXTRACT function is used to retrieve values from a specific location in a JSON object.

Example:

SELECT JSON_EXTRACT('{"name": "Alice", "age": 30, "position": "Developer"}', '$.name') AS extracted_value;

Explanation:

This query will return "Alice" because $.name specifies the path to the name key in the JSON document.

Output:

extracted_value
---------------
"Alice"

2. JSON_KEYS - Returns the Keys of a JSON Object

The JSON_KEYS function returns a JSON array containing the keys of the top-level object in the JSON data.

Example:

SELECT JSON_KEYS('{"name": "Alice", "age": 30, "position": "Developer"}') AS keys;

Explanation:

This query will return the JSON array ["name", "age", "position"], listing the keys at the top level of the JSON object.

Output:

keys
---------------------
["name", "age", "position"]

3. JSON_LENGTH - Returns the Length of a JSON Document (Array or Object)

The JSON_LENGTH function returns the number of elements in a JSON array or the number of keys in a JSON object.

Example with a JSON Object:

SELECT JSON_LENGTH('{"name": "Alice", "age": 30, "position": "Developer"}') AS object_length;

Explanation:

This query returns 3 because there are three keys in the JSON object: name, age, and position.

Output:

object_length
-------------
3

Example with a JSON Array:

SELECT JSON_LENGTH('["MySQL", "JavaScript", "Python"]') AS array_length;

Explanation:

This query returns 3 because the JSON array contains three elements.

Output:

array_length
------------
3

4. JSON_TYPE - Returns the Type of a JSON Value (e.g., Object, Array, String)

The JSON_TYPE function returns the data type of the JSON value located at the specified path.

Example:

SELECT JSON_TYPE('{"name": "Alice", "age": 30, "skills": ["MySQL", "JavaScript"]}', '$.skills') AS value_type;

Explanation:

This query returns ARRAY because the path $.skills points to a JSON array.

Output:

value_type
----------
ARRAY

5. JSON_CONTAINS - Checks if a JSON Document Contains a Specific Value

The JSON_CONTAINS function checks if a specified value exists within a JSON document.

Example:

SELECT JSON_CONTAINS('{"name": "Alice", "age": 30, "skills": ["MySQL", "JavaScript"]}', '"JavaScript"', '$.skills') AS contains_value;

Explanation:

This query returns 1 (true) if "JavaScript" exists in the skills array within the JSON document, or 0 (false) if it does not.

Output:

contains_value
--------------
1

Each of these functions allows for a deeper level of interaction with JSON data in MySQL, making it easy to store, retrieve, and validate structured data directly in the database.

7. Performance Considerations

Although JSON data types offer flexibility, there are some performance aspects to consider:

Indexing: JSON columns cannot be directly indexed, but generated columns allow you to extract values and index them.

Example:

ALTER TABLE employees 
ADD COLUMN position VARCHAR(50) AS (details->>'$.position'),
ADD INDEX (position);

This example adds a position column as a generated column, indexing it to improve query performance.

Storage: JSON data can consume more storage than regular structured columns. Use JSON for semi-structured data or dynamic attributes rather than core fields.

8. Best Practices

Use JSON Data Type for Dynamic or Semi-Structured Data: Use JSON for fields that may vary between rows or require flexibility, such as configuration data or additional attributes.

  • Limit JSON Nesting: Deeply nested JSON data can be challenging to query. Limit JSON data to two or three levels of depth.
  • Extract Key JSON Attributes: For frequently queried data, consider creating generated columns and indexing them.
  • Validate JSON: Ensure JSON data is correctly formatted before insertion.
  • Monitor Storage Usage: JSON data may take up more space. Monitor table size and optimize periodically.

Conclusion

MySQL’s JSON data type offers the flexibility to handle semi-structured data directly within the database, making it useful for applications needing dynamic and complex data structures. However, using JSON requires thoughtful structuring, especially around querying and indexing for optimal performance. By following best practices and leveraging MySQL’s built-in JSON functions, you can store and query JSON data efficiently.

Whether for storing configurations, logs, or other semi-structured data, MySQL’s JSON data type opens new possibilities for modern applications. Start experimenting with JSON in your MySQL database and see how it can simplify data management for your projects!

Checkout our instant dedicated servers and Instant KVM VPS plans.