From: Jian Shu, author: Wangcai doesn't cry
Link: https://www.jianshu.com/p/d4b012769a3b
After a long test, I will migrate to mysql8.0 as a whole; Mysql8.0 adds / optimizes many related JSON API operations for JSON operations; After reading the official documents, although most JSON operations are completed by the application layer, you can some JSON syntax of Mysql to facilitate debug ging; Select the basic and valuable parts for future reference;
https://dev.mysql.com/doc/refman/8.0/en/json.html
https://dev.mysql.com/doc/refman/8.0/en/json-utility-functions.html
Brief overview
-
Null is not allowed; JSON format definition is similar to LONGBLOB or LONGTEXT; Its maximum length is max_ allowed_ Controlled by packet;
-
When viewing the function of the amount of space occupied by the JSON field_ STORAGE_ SIZE(xxx);
-
In addition to ordinary Json operations, some related operations of GeoJSON (geospatial data exchange format based on geometry) are supported;
-
Support index for Json field (combined with the new features of Mysql8.0, the function index);
-
An optional optimization item that can support partial in-situ update of Json Column is added to MySQL 8.0; The functions you can use are JSON_SET(), JSON_REPLACE() , JSON_REMOVE(); When using, there are some constraints, but it will have more performance;
-
JSON basic tools;
//Using JSON_ARRAY method defines JSON array; SELECT JSON_ARRAY(1, "abc", NULL, TRUE, CURTIME()) //Result: [1, "abc", null, true, "11:30:24.000000"] //JSON_ The object method defines a JSON object SELECT JSON_OBJECT('id', 87, 'name', 'carrot') //Result {"id": 87, "name": "carrot"} //The scene where arrays and objects are nested; [99, {"id": "HK500", "cost": 75.99}, ["hot", "cold"]] {"k1": "value", "k2": [10, 20]} //Date / time type definition ["12:18:29.000000", "2015-07-29", "2015-07-29 12:18:29.000000"] //JSON_QUOTE escapes the JSON object into a String, that is, escapes the internal symbols and wraps them in double quotation marks as a whole; JSON_QUOTE(' "null" ') //Result '\ "null \" " //Beautify and output JSON content; JSON_PRETTY() //You can convert elements inside JSON/JSON into other data types; //As follows, convert the id element in JSON jdoc into unsigned int; [https://dev.mysql.com/doc/refman/8.0/en/json.html#json-converting-between-types] (https://dev.mysql.com/doc/refman/8.0/en/json.html#json-converting-between-types) ORDER BY CAST(JSON_EXTRACT(jdoc, '$.id') AS UNSIGNED);
-
Merge JSON operations JSON_MERGE_PRESERVE() and JSON_MERGE_PATCH() There is little possibility of actual business use;
-
->-- > operator, find the value according to the key; The difference is that -- > will remove the "of the package" and escape symbols; Its equivalent Function form is JSON_EXTRACT()
// {"mascot": "Our mascot is a dolphin named \"Sakila\"."} mysql> SELECT col->"$.mascot" FROM qtest; //Result: "Our mascot is a dolphin named \"Sakila \ "| SELECT sentence->>"$.mascot" FROM facts; // Result: | Our mascot is a dolphin named "Sakila"|
-
JSON Path expression
The content in the double quotation marks after -- > above is the so-called JSON Path expression;
This syntax is part of ECMAScript specification, so front-end programmers should be particularly familiar with it;
Take the following JSON as an example;
        [3, {"a": [5, 6], "b": 10}, [99, 100]]
        $[0] = 3 ;
        $[1] = {"a": [5, 6], "b": 10};
        $[2] = [99, 100];
At the same time, $[1] and $[2] are not scalars, and further
        $[1].a = [5,6]
        $[1].a[1] = 6
        $[1].b = 10;
        $[2][0] = 99;
Further supported syntax features $[n to m]
        $[ 1 to 2] = [{"a": [5, 6], "b": 10}, [99, 100]]
        $[last-2 to last-1] = [3, {"a": [5, 6], "b": 10}]
Summarize;
a. represents all members in object;
b [] represents all cells in array;
c [prefix] ** suffix represents all paths starting with prefix and ending with suffix;
- Find and modify JSON
//As above, it should be possible to use -- > syntax instead; mysql> SELECT JSON_EXTRACT('{"a": 1, "b": 2, "c": [3, 4, 5]}', '$.*'); //[1, 2, [3, 4, 5]] SELECT JSON_EXTRACT('{"a": 1, "b": 2, "c": [3, 4, 5]}', '$.c[*]') //[3, 4, 5] SELECT JSON_EXTRACT('{"a": {"b": 1}, "c": {"b": 2}}', '$**.b'); //[1, 2] SELECT JSON_EXTRACT('[1, 2, 3, 4, 5]', '$[1 to 3]'); //[2, 3, 4] //JSON_SET JSON_INSERT JSON_REPLACE JSON_REMOVE SET @j = '["a", {"b": [true, false]}, [10, 20]]'; SELECT JSON_SET(@j, '$[1].b[0]', 1, '$[2][2]', 2); //| ["a", {"b": [1, false]}, [10, 20, 2]] SELECT JSON_INSERT(@j, '$[1].b[0]', 1, '$[2][2]', 2); //["a", {"b": [true, false]}, [10, 20, 2]] JSON_REPLACE(@j, '$[1].b[0]', 1, '$[2][2]', 2) //["a", {"b": [1, false]}, [10, 20]] SELECT JSON_REMOVE(@j, '$[2]', '$[1].b[1]', '$[1].b[1]'); //["a", {"b": [true]}]
- JSON Table Functions A common scenario is that JSON data itself is a table structure;
JSON_TABLE(*expr*, *path* COLUMNS (*column_list*) [AS] *alias*)
SELECT * FROM JSON_TABLE( '[{"a":"3"},{"a":2},{"b":1},{"a":0},{"a":[1,2]}]', -> "$[*]" -> COLUMNS( -> rowid FOR ORDINALITY, -> ac VARCHAR(100) PATH "$.a" DEFAULT '111' ON EMPTY DEFAULT '999' ON ERROR, -> aj JSON PATH "$.a" DEFAULT '{"x": 333}' ON EMPTY, -> bx INT EXISTS PATH "$.b" -> ) -> ) AS tt;
-
Comparison and Ordering of JSON Values
I don't think it's worth it at present; -
Aggregation of JSON Values
I don't think it's worth it at present; Aggregate functions can be used by converting the return value to other types;