Efficiently Handling Array-like Data in MySQL Using JSON Types

目次

1. Introduction

The need to handle array-style data in MySQL

In databases, data is typically stored under a relational design. However, depending on application requirements, it may be useful to store multiple values in one column. In such cases a data structure like an “array” can help.

For example, consider the following cases:

  • Storing multiple tags selected by a user.
  • Saving multiple image URLs for a product.
  • Consolidating history or logs into one field.

Advantages of leveraging the JSON type

MySQL does not provide a direct “array type”, but by using the JSON type, you can handle array-style data. The JSON type is highly flexible and provides the following advantages:

  • Supports nested data structures.
  • Allows data manipulation directly within queries.
  • Manages multiple data formats within one field.

In this article we will introduce how to efficiently handle array-style data in MySQL using the JSON type.

2. Basics of handling arrays with MySQL’s JSON type

What is the JSON type?

JSON (JavaScript Object Notation) is a lightweight and simple data interchange format. MySQL supports a native JSON type from version 5.7 onward, allowing you to store and manipulate JSON-formatted data directly in the database.

Example: the following is data storable in a JSON type field:

{
  "tags": ["PHP", "MySQL", "JSON"],
  "status": "published"
}

Benefits and use-cases of the JSON type

The main advantages of using the JSON type are:

  1. Flexible data structure: You can handle variable-length data without altering the relational schema.
  2. Efficient data manipulation: Use MySQL’s built-in functions (e.g. JSON_EXTRACT, JSON_ARRAY) to easily manipulate data.
  3. Schema-less design possible: You don’t need to modify schema frequently as application specifications change.

Use-cases:

  • Assign multiple categories to product information.
  • Store custom user settings.
  • Use in web applications processing nested JSON data.

3. Basic operations on JSON arrays

Creating JSON arrays

In MySQL you can use the JSON_ARRAY function to easily create JSON-formatted arrays. Arrays are helpful when you want to store multiple values within one column.

Example usage

In the following query we create a JSON array named tags.

SELECT JSON_ARRAY('PHP', 'MySQL', 'JavaScript') AS tags;

Result:

["PHP", "MySQL", "JavaScript"]

Applied example

Here is an example storing a JSON array into the database via an INSERT statement.

CREATE TABLE articles (
    id INT AUTO_INCREMENT PRIMARY KEY,
    tags JSON
);

INSERT INTO articles (tags) 
VALUES (JSON_ARRAY('PHP', 'MySQL', 'JavaScript'));

Extracting data from JSON arrays

To retrieve data stored in a JSON array you use the JSON_EXTRACT function. It allows you to extract specific elements from within the array.

Example usage

In the following example we retrieve the second element (0-based index) of the array.

SELECT JSON_EXTRACT('["PHP", "MySQL", "JavaScript"]', '$[1]') AS second_tag;

Result:

"MySQL"

Retrieve multiple elements

You can also obtain multiple elements at once.

SELECT JSON_EXTRACT('["PHP", "MySQL", "JavaScript"]', '$[0]', '$[2]') AS extracted_values;

Add, update, and delete data

Adding data to an array

Using the JSON_ARRAY_APPEND function you can append new data to an existing array.

SET @tags = '["PHP", "MySQL"]';
SELECT JSON_ARRAY_APPEND(@tags, '$', 'JavaScript') AS updated_tags;

Result:

["PHP", "MySQL", "JavaScript"]

Updating data within an array

You can update a specific element inside the array using JSON_SET.

SET @tags = '["PHP", "MySQL", "JavaScript"]';
SELECT JSON_SET(@tags, '$[1]', 'Python') AS updated_tags;

Result:

["PHP", "Python", "JavaScript"]

Removing data from an array

Use JSON_REMOVE to delete a specific element inside the array.

SET @tags = '["PHP", "MySQL", "JavaScript"]';
SELECT JSON_REMOVE(@tags, '$[1]') AS updated_tags;

Result:

["PHP", "JavaScript"]

4. Searching and filtering JSON arrays

Searching for arrays containing specific data

To determine whether a JSON array contains a specific value you use the JSON_CONTAINS function. This function returns whether the specified JSON array contains the given value.

Example usage

In the following example we check if the JSON array contains “MySQL”.

SELECT JSON_CONTAINS('["PHP", "MySQL", "JavaScript"]', '"MySQL"') AS is_present;

Result:

1  (if present)
0  (if not present)

Applied example: conditional search

If you want to query rows in the database that hold a JSON array containing a specific value, you can use JSON_CONTAINS in the WHERE clause.

SELECT * 
FROM articles
WHERE JSON_CONTAINS(tags, '"MySQL"');

This query retrieves rows where the tags column includes “MySQL”.

Getting the length of an array

To get the number of elements in a JSON array use the JSON_LENGTH function. This is useful for data analysis or conditional filtering.

Example usage

In the following example we get the number of elements in the array.

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

Result:

3

Practical example: extracting rows that meet a condition

To extract rows where the number of elements is equal to or greater than a certain value use JSON_LENGTH in the WHERE clause.

SELECT * 
FROM articles
WHERE JSON_LENGTH(tags) >= 2;

This query selects rows where the tags column contains two or more elements.

Practical example with conditional queries

You can combine multiple conditions for more advanced searches. The following query finds rows where the tags array contains “JavaScript” and has three or more elements.

SELECT * 
FROM articles
WHERE JSON_CONTAINS(tags, '"JavaScript"') 
  AND JSON_LENGTH(tags) >= 3;

5. Practical use-cases: learning via real scenarios for JSON arrays

How to store product categories as a JSON array

In e-commerce sites products may belong to multiple categories. In these cases using a JSON array to store category information can be efficient.

Example: storing category data for products

Below is an example of creating a table with a JSON column named categories and storing multiple categories.

CREATE TABLE products (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    categories JSON
);

INSERT INTO products (name, categories) 
VALUES ('Laptop', JSON_ARRAY('Electronics', 'Computers')),
       ('Smartphone', JSON_ARRAY('Electronics', 'Mobile Devices'));

This data structure allows succinct storage even when a product belongs to multiple categories.

Querying products belonging to a specific category

By leveraging the JSON type, you can easily search for products belonging to specific categories.

Query example

The following query finds all products in the “Electronics” category.

SELECT name 
FROM products
WHERE JSON_CONTAINS(categories, '"Electronics"');

Result:

Laptop
Smartphone

This query lets you flexibly retrieve lists of products by category.

Example: filtering by price range

Let’s see how to store price information in JSON and query products based on a price range.

Data example

Below we store product price information per item using the JSON type.

CREATE TABLE products_with_prices (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    details JSON
);

INSERT INTO products_with_prices (name, details)
VALUES ('Laptop', '{"price": 150000, "categories": ["Electronics", "Computers"]}'),
       ('Smartphone', '{"price": 80000, "categories": ["Electronics", "Mobile Devices"]}');

Query example

To search for products priced at 100,000 or more, you use JSON_EXTRACT.

SELECT name 
FROM products_with_prices
WHERE JSON_EXTRACT(details, '$.price') >= 100000;

Result:

Laptop

Using JSON_TABLE for expansion and query examples

When you want to query JSON data in relational form you can use JSON_TABLE. This function lets you expand a JSON array as a virtual table.

Example usage

Below is an example where a JSON array is expanded and each category is displayed as a separate row.

SELECT * 
FROM JSON_TABLE(
    '["Electronics", "Computers", "Mobile Devices"]',
    '$[*]' COLUMNS(
        category_name VARCHAR(100) PATH '$'
    )
) AS categories_table;

Result:

category_name
--------------
Electronics
Computers
Mobile Devices

6. Cautions when using the JSON type

Performance optimization points

Though the JSON type is highly flexible, without proper design it can negatively affect database performance. Below are key performance-optimization points.

1. Use of indexes

In MySQL you cannot directly set an index on a JSON column itself, but you can create a virtual column and index a specific key.

Example: creating index via virtual column

In this example we index the price key inside JSON data.

ALTER TABLE products_with_prices
ADD COLUMN price INT AS (JSON_EXTRACT(details, '$.price')) STORED,
ADD INDEX idx_price (price);

By using a virtual column you can greatly improve search performance on JSON type data.

2. Avoid overly complex JSON structures

Deeply nested JSON structures affect readability of queries and performance. When designing data, adopt as simple a JSON structure as possible.

Good example:

{
  "categories": ["Electronics", "Computers"],
  "price": 150000
}

Structure to avoid:

{
  "product": {
    "details": {
      "price": 150000,
      "categories": ["Electronics", "Computers"]
    }
  }
}

How to use indexes effectively

When using virtual columns for index creation, the following points must be considered:

  1. The virtual column must be STORED.
  2. Use JSON_EXTRACT to extract specific keys into the virtual column.

For example, to extract the categories key value and index it:

ALTER TABLE products
ADD COLUMN main_category VARCHAR(255) AS (JSON_EXTRACT(categories, '$[0]')) STORED,
ADD INDEX idx_main_category (main_category);

The importance of data validation

Although JSON type data is flexible, it also risks storing incorrect formats. To maintain data integrity, use the following approaches.

1. Use CHECK constraints

From MySQL 8.0 onward you can use CHECK constraints to validate JSON data structure or content.

ALTER TABLE products_with_prices
ADD CONSTRAINT check_price CHECK (JSON_EXTRACT(details, '$.price') >= 0);

2. Validation at the application level

When inserting data you should validate JSON format at the application level. In programming languages like PHP or Python you can use standard libraries to validate JSON.

7. Frequently asked questions about using array-style data in MySQL

Q1: Does MySQL provide an array type?

A1: MySQL does not have a direct “array type”. However you can handle array-style data by using the JSON type. Using the JSON type you can store multiple values in one column and manipulate them through queries.

Example:

SELECT JSON_ARRAY('value1', 'value2', 'value3') AS example_array;

Result:

["value1", "value2", "value3"]

Q2: Can you set an index on JSON type data?

A2: You cannot directly put an index on a JSON type itself. However you can extract specific keys or values as a virtual column and then set an index on that column.

Example:

ALTER TABLE products_with_prices
ADD COLUMN price INT AS (JSON_EXTRACT(details, '$.price')) STORED,
ADD INDEX idx_price (price);

This enables efficient search for values inside JSON data.

 

Q3: Is there a size limit for JSON data?

A3: MySQL’s JSON type can store up to 4 GB of data. However using very large JSON data may degrade performance, so proper data design is essential.

Recommendations:

  • Store only minimum required data.
  • Avoid deeply nested JSON structures.

 

Q4: How do you update a specific element in a JSON array?

A4: You update specific elements inside a JSON array with the JSON_SET function.

Example:

SET @tags = '["PHP", "MySQL", "JavaScript"]';
SELECT JSON_SET(@tags, '$[1]', 'Python') AS updated_tags;

Result:

["PHP", "Python", "JavaScript"]


Q5: Comparison between JSON type and traditional table design

A5: The JSON type offers high flexibility but also differs in nature from traditional relational design.

ItemJSON typeTraditional table design
FlexibilityHigh (no schema changes required)Fixed (schema changes needed)
PerformanceInferior in some operationsOptimized
Query complexityRequires JSON functionsSimple
IndexingPartially supported via virtual columnsFully supported

8. Summary

Benefits of handling array-style data with the JSON type in MySQL

This article explained the JSON type for handling array-style data in MySQL. The major points covered here are:

  1. Why use the JSON type
    MySQL lacks a direct array type, but by using the JSON type you can store multiple values in one column and perform flexible data operations.
  2. Basic JSON operations
  • Learned how to create JSON arrays, extract data, update and delete values.
  • Used functions such as JSON_ARRAY, JSON_EXTRACT, and JSON_SET to manipulate array-style data efficiently.
  1. Searching and filtering
  • Used JSON_CONTAINS to search for specific values inside JSON arrays.
  • Used JSON_LENGTH to obtain array length and perform conditional filtering.
  1. Practical use-cases
    We learned concrete application scenarios such as managing product categories and filtering by price.
  2. Cautions and optimization
  • Discussed how to index JSON data via virtual columns and the importance of validating JSON data.

Next steps for leveraging the JSON type

By using the JSON type in MySQL you can enable more flexible data management than traditional relational database design. However proper design and performance considerations are essential.

Topics to learn next:

  • Utilizing composite indexes
    Designing indexes combining JSON type columns with regular columns.
  • Advanced JSON functions
    Using JSON_MERGE, JSON_OBJECT and other functions for more complex operations.
  • Application-level data manipulation
    Efficiently manipulating MySQL JSON data using languages such as PHP or Python.

Final summary

Through this article you have learned how to handle array-style data efficiently using the JSON type in MySQL. By applying this knowledge you can design more flexible and scalable database systems.