Talking about MySQL JSON data type again

This article was first published on the WeChat public account "andyqian". Looking forward to your attention~

foreword

In the blink of an eye, the article has not been updated for a short while. Alas, lazy again... Do you remember the article " Talking about MySQL JSON Data Types " written before, in this article, we briefly introduced MySQL JSON functions. But in practice. There are more practical operations not introduced. Such as: How to search JSON string? How to get a specific property in a JSON string? Is it possible to batch replace specific keys in JSON? etc. Don't worry, we'll give you the answer below.

data preparation

The data looks like this:

create table t_base_data(   
    id bigint(20) not null primary key auto_increment comment "主键",
    content json null comment "内容",
    created_at datetime null comment "创建时间",
    updated_at datetime null comment "修改时间"
) engine=innodb charset=utf8

-- 初始化数据:
INSERT INTO `t_base_data` (`id`, `content`, `created_at`, `updated_at`) VALUES ('1', '{\"blog\": \"www.andyqian.com\", \"name\": \"andyqian\"}', NULL, '2018-04-08 11:19:44');

Operation database version: 5.7.20

search

When we need to get a certain key value in the JSON string. We can do this by:

  1. column name->key

As follows:

mysql> select content->"$.blog" from t_base_data;
+--------------------+
| content->"$.blog"  |
+--------------------+
| "www.andyqian.com" |
+--------------------+
1 row in set (0.00 sec

2. Column name ->>key provides escaped characters.
As follows:

mysql> select content->>"$.blog" from t_base_data;
+--------------------+
| content->>"$.blog" |
+--------------------+
| www.andyqian.com   |
+--------------------+
1 row in set (0.00 sec)
  1. JSON_EXTRACT function

Function: JSON_EXTRA (JSON string, operator).
Purpose: User extracts JSON properties.

Example of use:

mysql> SELECT JSON_EXTRACT('{"blog":"www.andyqian.com","name": "andyqian"}' ,'$.blog');
+--------------------------------------------------------------------------+
| JSON_EXTRACT('{"blog":"www.andyqian.com","name": "andyqian"}' ,'$.blog') |
+--------------------------------------------------------------------------+
| "www.andyqian.com"                                                       |
+--------------------------------------------------------------------------+
1 row in set (0.00 sec)

json_insert

In Java, to append strings, we can usually use the append() method to operate. In MySQL, varchar data types, we can also use the concat function to append. Similarly, in JSON operations, MySQL also provides us with the corresponding function: JSON_INSERT.
The usage example is as follows:

mysql> set @info = '{"blog":"www.andyqian.com","name": "andyqian"}';
Query OK, 0 rows affected (0.00 sec)

mysql> select json_insert(@info,"$.email","[email protected]");
+-----------------------------------------------------------------------------------+
| json_insert(@info,"$.email","[email protected]")                               |
+-----------------------------------------------------------------------------------+
| {"blog": "www.andyqian.com", "name": "andyqian", "email": "[email protected]"} |
+-----------------------------------------------------------------------------------+
1 row in set (0.01 sec)

Note here: If the new property already exists. This property will not be added.

json_replace

There are new additions. When bulk modifying the value of the specified JSON content. Replacement is inevitable. Now we introduce the following function json_replace function.
The usage method is as follows:

mysql> set @info = '{"blog":"www.andyqian.com","name": "andyqian"}';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT json_replace(@info, '$.name', "juqian");
+------------------------------------------------+
| json_replace(@info, '$.name', "juqian")        |
+------------------------------------------------+
| {"blog": "www.andyqian.com", "name": "juqian"} |
+------------------------------------------------+
1 row in set (0.00 sec)

This statement means: we need to replace

{"blog":"www.andyqian.com","name": "andyqian"}

The value of the name attribute in juyuqian.

Note: Above we are directly using the name of the key to search. actually. We can also use the corner mark method. to find it. For example, in the above statement, we can $.namemodify it as $[1]well.

json_remove

With additions, replacements, and of course deletions. Next we introduce the json_remove function

mysql> select json_remove('{"blog":"www.andyqian.com","name": "andyqian"}' , '$.blog');
+--------------------------------------------------------------------------+
| json_remove('{"blog":"www.andyqian.com","name": "andyqian"}' , '$.blog') |
+--------------------------------------------------------------------------+
| {"name": "andyqian"}                                                     |
+--------------------------------------------------------------------------+
1 row in set (0.00 sec)

We only need to specify the key value of remove.

finally

If you are dealing with the above JSON operations, you will not be able to get used to it all of a sudden. We can think of it as manipulating fastjson in Java to perform JSON operations. It's easier to understand.

Related Reading:

Talk about Java logs

Talk about a few small details that drag down the system!

Talking about MySQL table structure design

Talk about MySQL JSON data type

 

write picture description here

 Scan the code to follow and make progress together

Personal blog:  http://www.andyqian.com

Guess you like

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