JSON Data Type in MySQL 8: Tutorial & Examples

Updated: January 26, 2024 By: Guest Contributor Post a comment

Introduction

With the rise of NoSQL databases, traditional relational database systems like MySQL have evolved to incorporate features that allow developers to store and work with JSON (JavaScript Object Notation) data. MySQL 8.0 onwards, the native JSON data type is included to store JSON documents more efficiently. This tutorial covers the JSON data type in MySQL 8, including a range of examples from basic to advanced.

Getting Started with JSON in MySQL

The JSON data type provides a convenient way to store and manage structured JSON documents within a MySQL table. Let’s first understand how to create a table with a JSON column.

Creating a Table with a JSON Column

CREATE TABLE users (
  id INT AUTO_INCREMENT PRIMARY KEY,
  profile JSON,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

In the above code, we define a table named users with an auto-incremented id, a profile column for storing JSON objects, and a created_at timestamp.

Inserting JSON Data

INSERT INTO users (profile) VALUES
('{"name":"John", "age":30, "city":"New York"}'),
('{"name":"Jane", "age":25, "city":"Los Angeles"}');

This illustrates how to insert JSON formatted as a string into the profile column. Ensure that the JSON string is well-formed; otherwise, MySQL will return an error.

Querying JSON Data

MySQL 8 allows you to extract and manipulate JSON data directly using SQL queries.

Selecting JSON Data

SELECT profile->>'$.name' AS name FROM users;

The arrow operator (->>) is used to extract a value from a JSON document. In this case, we’re extracting the name attribute from the profile column.

Working with JSON Paths

JSON paths specify the location of data within a JSON document. For example, '$.name' indicates the name key in the root object.

Modifying JSON Data

UPDATE users SET profile = JSON_SET(profile, '$.city', 'Boston')
WHERE JSON_EXTRACT(profile, '$.name') = 'John';

Here we use JSON_SET to update the city for the user named John. JSON_EXTRACT is used in the WHERE clause to identify the correct record.

Advanced JSON Features

MySQL 8 also includes advanced features for working with JSON documents, such as validating JSON, merging documents, and more.

Validating JSON Data

SELECT JSON_VALID('{"name":"John", "age":"invalid"}') AS is_valid;

This function checks whether the string is a valid JSON document. In this case, the output will be 0 (false), since age should be a number and not a string.

Merging JSON Documents

SELECT JSON_MERGE_PATCH(profile, '{"country":"USA"}') AS updated_profile FROM users;

The JSON_MERGE_PATCH function merges two JSON documents, adding a country key in this example.

Generating JSON Arrays

SELECT JSON_ARRAYAGG(profile->>'$.name') AS user_names FROM users;

Here we generate a JSON array of user names with JSON_ARRAYAGG.

Indexing JSON Data

Indexing JSON data can improve the performance of queries that search within JSON columns.

Creating an Index on a JSON Column

CREATE INDEX idx_name ON users((profile->>'$.name'));

This will create an index named idx_name using the name value within the profile.

Conclusion

To summarize, the JSON data type in MySQL 8 provides powerful capabilities to store and query JSON documents efficiently in a relational database context. This tutorial introduced the basics of JSON storage, query techniques, and some advanced operations. You now possess the knowledge to integrate JSON data types within your MySQL databases, enabling you to build more flexible and rich applications.