MySQL5.7 JSON type usage introduction

JSON is a lightweight data interchange format that uses a language-independent text format, similar to XML, but simpler, easier to read and easier to write than XML. It is easy for machines to parse and generate, and it will reduce the transmission of network bandwidth.
    The format of JSON is very simple: name/key value. To achieve such storage in the previous version of MySQL, either VARCHAR or TEXT large text was used. After the release of MySQL 5.7, the JSON data type and the retrieval of this type and other function parsing were specially designed. Let's first look at JSON access in older versions of MySQL.

CREATE TABLE `json_test` (
  `id` int(11) DEFAULT NULL,
  `person_desc` text
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

 Let's insert a record:

INSERT INTO json_test VALUES (1,'{
    "programmers": [{
        "firstName": "Brett",
        "lastName": "McLaughlin",
        "email": "aaaa"
    }, {
        "firstName": "Jason",
        "lastName": "Hunter",
        "email": "bbbb"
    }, {
        "firstName": "Elliotte",
        "lastName": "Harold",
        "email": "cccc"
    }],
    "authors": [{
        "firstName": "Isaac",
        "lastName": "Asimov",
        "genre": "sciencefiction"
    }, {
        "firstName": "Tad",
        "lastName": "Williams",
        "genre": "fantasy"
    }, {
        "firstName": "Frank",
        "lastName": "Peretti",
        "genre": "christianfiction"
    }],
    "musicians": [{
        "firstName": "Eric",
        "lastName": "Clapton",
        "instrument": "guitar"
    }, {
        "firstName": "Sergei",
        "lastName": "Rachmaninoff",
        "instrument": "piano"
    }]
}');

 Generally, if we encounter this way of storing JSON format, we can only take this record out and hand it over to an application, and an application will parse it.

 

Now to MySQL 5.7, we re-modify the following table structure:

ALTER TABLE json_test MODIFY person_desc json;

 Let's take a look at the keys of the inserted JSON data:

SELECT id,json_keys(person_desc) as "keys" FROM json_test

We can see that there are three KEYs in it, namely authors, musicians, and programmers. Now find a KEY and take out the corresponding value:

mysql> SELECT json_extract(AUTHORS,'$.lastName[0]') AS 'name', AUTHORS FROM
    -> (
    -> SELECT id,json_extract(person_desc,'$.authors[0][0]') AS "authors" FROM json_test
    -> UNION ALL
    -> SELECT id,json_extract(person_desc,'$.authors[1][0]') AS "authors" FROM json_test
    -> UNION ALL
    -> SELECT id,json_extract(person_desc,'$.authors[2][0]') AS "authors" FROM json_test
    -> ) AS T1
    -> ORDER BY NAME DESC\G
*************************** 1. row ***************************
   name: "Williams"
AUTHORS: {"genre": "fantasy", "lastName": "Williams", "firstName": "Tad"}
*************************** 2. row ***************************
   name: "Peretti"
AUTHORS: {"genre": "christianfiction", "lastName": "Peretti", "firstName": "Frank"}
*************************** 3. row ***************************
   name: "Asimov"
AUTHORS: {"genre": "sciencefiction", "lastName": "Asimov", "firstName": "Isaac"}

3 rows in set (0.00 sec)

 Now to list the detailed values:

mysql> SELECT
    -> json_extract(AUTHORS,'$.firstName[0]') AS "firstname",
    -> json_extract(AUTHORS,'$.lastName[0]') AS "lastname",
    -> json_extract(AUTHORS,'$.genre[0]') AS "genre"
    -> FROM
    -> (
    -> SELECT id,json_extract(person_desc,'$.authors[0]') AS "authors" FROM json
_test
    -> ) AS T\G
*************************** 1. row ***************************
firstname: "Isaac"
 lastname: "Asimov"
    genre: "sciencefiction"
1 row in set (0.00 sec)

 Let's further demonstrate the deletion of all objects corresponding to the KEY of authors.

mysql> UPDATE json_test
    -> SET person_desc = json_remove(person_desc,'$.authors')\G
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

 Find the corresponding KEY and find that it has been deleted.

mysql> SELECT json_contains_path(person_desc,'all','$.authors') as authors_exist
s FROM json_test\G
*************************** 1. row ***************************
authors_exists: 0
1 row in set (0.00 sec)

To sum up, although MySQL 5.7 starts to support the JSON data type, I suggest that if you want to use it, it is best to take out such a value and calculate it in the application section , after all, the database is used to process simple data.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326486131&siteId=291194637