[MySQL] How to query and filter stored JSON data in MySQL?

1. Background introduction

JSON (JavaScript Object Notation) is a lightweight data exchange format that is widely used in modern applications. Processing and storing unstructured data is becoming increasingly important. MySQL is a commonly used relational database management system. In order to support the storage and query of unstructured data, MySQL introduces support for the JSON data type.

MySQL has added support for JSON starting from version 5.7.8, allowing us to store and query data in JSON format in the MySQL database. This article will introduce how to use MySQL to query and filter stored JSON data, and provide corresponding code examples.

2. Supported JSON data types

Starting with MySQL version 5.7.8, MySQL introduced support for the JSON data type. MySQL can store any JSON data, including objects, arrays, strings, etc. And MySQL also provides a series of operation functions and operators for the JSON data type to facilitate the query and operation of JSON data.

3. Basic data

3.1 Create table

DROP TABLE IF EXISTS `my_table`;
CREATE TABLE `my_table`  (
  `id` int NOT NULL,
  `message` json NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;

The above example creates a my_tabletable named with a messageJSON column named . messageColumns can store arbitrary JSON data.

3.2 Insert JSON data

To insert data in a JSON column, you can use the JSON function to convert a JSON string to a JSON data type. Here is an example:

INSERT INTO `my_table` (`id`, `message`) VALUES (1, '{\"age\": 30, \"name\": \"John\"}');
INSERT INTO `my_table` (`id`, `message`) VALUES (2, '{\"age\": 40, \"name\": \"Mary\"}');
INSERT INTO `my_table` (`id`, `message`) VALUES (3, '{\"age\": 50, \"name\": \"Tom\"}');
INSERT INTO `my_table` (`id`, `message`) VALUES (4, '{\"age\": 60, \"name\": \"Jermy\"}');

The above example my_tableinserts 4 records into the table, where messagethe column contains a JSON object.

3.3 Query JSON data

You can use the -> or ->> operator to query JSON data. The -> operator returns the attribute value of the JSON object, and the ->> operator returns the string representation of the attribute value.

Here are two

Example 1 of querying JSON data:

SELECT message->'$.name' AS name FROM my_table;

The result is as follows:

Insert image description here

Query example 2:

SELECT * FROM my_table WHERE JSON_EXTRACT(message, '$.age') > 40

The result is as follows:

Insert image description here

4. Operation function

MySQL provides a series of JSON data operation functions to facilitate query and operation of JSON data. The following are some commonly used JSON data manipulation functions:

4.1 JSON_OBJECT

JSON_OBJECTFunction is used to create a JSON object. Here is an example:

SELECT JSON_OBJECT('name', 'John', 'age', 30) AS message;

The above example creates a messageJSON object named containing name and age properties.

Insert image description here

4.2 JSON_ARRAY

JSON_ARRAYFunction is used to create a JSON array. Here is an example:

SELECT JSON_ARRAY('apple', 'banana', 'orange') AS fruits;

The above example creates a JSON array named fruits containing three elements.

Insert image description here

4.3 JSON_EXTRACT

JSON_EXTRACTFunctions are used to extract values ​​from JSON data. Here is an example:

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

The above example extracts the value of the name attribute from JSON data.

Insert image description here

The above are just the most basic usages. There are many advanced usages of MySQL functions for operating JSON data types. You can learn them when you need them.

This article is finished!

Guess you like

Origin blog.csdn.net/weixin_44299027/article/details/135371750