"Original" MySQL5.7 JSON type usage Introduction

Source: Internet
Author: User

JSON is a lightweight data interchange format that takes a language-independent text format, like XML, but is simpler, easier to read and easier to write than XML. It is easy for the machine to parse and generate, and will reduce the transmission of network bandwidth.

The format of JSON is very simple: name/key value. Before the MySQL version to implement such a storage, either with varchar or with text large text. After the release of MySQL5.7, the JSON data types were specifically designed, as well as about this type of retrieval and other function parsing. Let's look at the old MySQL version of JSON access first.


Sample Table structure:

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

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


That's what we're going to do. In order to store the JSON format, we can only take this record out to the application and have an application to parse it.



Now to the MySQL5.7, we re-modify the following table structure:

ALTER TABLE json_test MODIFY person_desc json;


Let's take a look at the key 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)


As we can see, there are three keys in it, each of which is authors,musicians,programmers. Now find a key to take the corresponding value out:

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 out 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-and select Id,js On_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 further demonstrate that all objects corresponding to the authors key are deleted.

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 out the corresponding key, the discovery 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)



In summary, although MySQL5.7 begins to support JSON data types, I suggest that if you want to use it, it is best to take this value out and then calculate it in the application segment, after all, the database is used to process simple data.


This article is from "God, we don't see!" "Blog, be sure to keep this provenance http://yueliangdao0608.blog.51cto.com/397025/1711454

"Original" MySQL5.7 JSON type usage Introduction

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.