[Original] Introduction to MySQL5.7JSON

Source: Internet
Author: User
JSON is a lightweight data exchange format, which uses a language-independent text format, similar to XML, but easier to read and write than XML. It is easy for machines to parse and generate, and reduces network bandwidth transmission. JSON format is very simple: name key value. In MySQL earlier versions, you must either use VARCHAR or T

JSON is a lightweight data exchange format, which uses a language-independent text format, similar to XML, but easier to read and write than XML. It is easy for machines to parse and generate, and reduces network bandwidth transmission. JSON format is very simple: Name/key value. In MySQL earlier versions, you must either use VARCHAR or T

JSON is a lightweight data exchange format, which uses a language-independent text format, similar to XML, but easier to read and write than XML. It is easy for machines to parse and generate, and reduces network bandwidth transmission.

JSON format is very simple: Name/key value. In MySQL earlier versions, such storage must be implemented by using either VARCHAR or TEXT. After MySQL5.7 was released, it specifically designed the JSON data type, retrieval of this type, and other function parsing. Let's take a look at JSON access in earlier MySQL versions.


Example Table Structure:

CREATE TABLE json_test(id INT,person_desc TEXT)ENGINE INNODB;

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"    }]}');


In this case, we can only extract this record and submit it to an application for parsing.



Now we have reached MySQL5.7. Let's re-Modify the table structure:

ALTER TABLE json_test MODIFY person_desc json;


First, let's take a look at the keys of the inserted JSON data:

mysql> SELECT id,json_keys(person_desc) as "keys" FROM json_test\G*************************** 1. row ***************************  id: 1keys: ["authors", "musicians", "programmers"]1 row in set (0.00 sec)


We can see that there are three keys, namely authors, musicians, and programmers. 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 let's 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)


We will further demonstrate removing all objects corresponding to the authors KEY.

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

Find the corresponding KEY and find it has been deleted.

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



To sum up, although MySQL5.7 supports the JSON data type, we recommend that you extract the value and calculate it in the Application Section, after all, databases are used to process simple data.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.