Overview
MySQL introduced JSON support in version 5.7, and continued to enhance it in subsequent versions.
Official document:
JSON
JSON-Functions
Introduction
Name | Description |
---|---|
JSON_APPEND() | Append data to JSON document |
JSON_ARRAY() | Create JSON array |
JSON_ARRAY_APPEND() | Append data to JSON document |
JSON_ARRAY_INSERT() | Insert into JSON array |
-> |
Return value from JSON column after evaluating path; equivalent to JSON_EXTRACT(). |
JSON_CONTAINS() | Whether JSON document contains specific object at path |
JSON_CONTAINS_PATH() | Whether JSON document contains any data at path |
JSON_DEPTH() | Maximum depth of JSON document |
JSON_EXTRACT() | Return data from JSON document |
->> |
Return value from JSON column after evaluating path and unquoting the result; equivalent to JSON_UNQUOTE(JSON_EXTRACT()). |
JSON_INSERT() | Insert data into JSON document |
JSON_KEYS() | Array of keys from JSON document |
JSON_LENGTH() | Number of elements in JSON document |
JSON_MERGE() | Merge JSON documents |
JSON_OBJECT() | Create JSON object |
JSON_QUOTE() | Quote JSON document |
JSON_REMOVE() | Remove data from JSON document |
JSON_REPLACE() | Replace values in JSON document |
JSON_SEARCH() | Path to value within JSON document |
JSON_SET() | Insert data into JSON document |
JSON_TYPE() | Type of JSON value |
JSON_UNQUOTE() | Unquote JSON value |
JSON_VALID() | Whether JSON value is valid |
All the above functions can be applied to fields of json, text, varchar, etc. $
Represents the entire json object, use subscripts (for json array, starting from 0) or key values (for json object, the key containing special characters should be "
enclosed, such as $. "my name") when indexing data .
The data in json can be compared with =, <, <=, >, >=, <>, !=, and <=>. But the data types in json can be diverse, so when comparing between different types, there is a priority, and the high priority is greater than the low priority (you can use the JSON_TYPE() function to view the type). The priority from high to low is as follows:
BLOB
BIT
OPAQUE
DATETIME
TIME
DATE
BOOLEAN
ARRAY
OBJECT
STRING
INTEGER, DOUBLE
NULL
Function classification
Functions That Create JSON Values
- JSON_ARRAY
- JSON_OBJECT
- JSON_QUOTE
- CONVERT
Functions That Search JSON Values
-
JSON_CONTAINS
-
JSON_CONTAINS_PATH
-
JSON_EXTRACT
json_extract(<field>,'$.name')
; -
JSON_KEYS
-
JSON_SEARCH
Functions That Modify JSON Values
-
JSON_APPEND
-
JSON_ARRAY_APPEND
-
JSON_ARRAY_INSERT
-
JSON_INSERT
-
JSON_REPLACE
-
JSON_SET
-
JSON_MERGE
JSON_MERGE(json_doc, json_doc[, json_doc] ...)
merge multiple json documents. Rule:
If both are json arrays, the result will be automatically merged into a json array;
if they are all json objects, the result will be automatically merged into a json object;
if there are multiple types, encapsulate the non-json array elements into a json array and then Follow rule one for mege. -
JSON_REMOVE
-
JSON_UNQUOTE
Functions That Return JSON Value Attributes
-
JSON_DEPTH
gets the depth of the json document. If the parameter is NULL, NULL is returned. The depth of an empty json array, json object or scalar is 1.
SELECT JSON_DEPTH('[10, {"a": 20}]');
Output: 3 -
JSON_LENGTH
JSON_LENGTH(json_doc[, path])
gets the length under the specified path. If the parameter is NULL, NULL is returned. Length calculation rules:
the length of the scalar is 1; the length of the
json array is the number of elements;
the length of the json object is the number of keys.
SELECT JSON_LENGTH('{"a": 1, "b": {"c": 30}}', '$.b'); output: 1
-
JSON_TYPE
gets the specific type of json document -
JSON_VALID
judges whether val is a valid json format, it is 1, not 0. If the parameter is NUL, NULL is returned.
$.*
Return all json
$.title
return key='title' data
$**.text
return all bottom-level key='text' data
$.content[*].item1[*]
return key=content list key=item1 list all contents
json:
- json type field can be NULL
- When inserting data, the json type field must be a valid json string
- You can use the JSON_OBJECT() function to construct a json object
- Use the JSON_ARRAY() function to construct a json array
Instance
JSON_MERGE with group by
reference
https://www.cnblogs.com/waterystone/p/5626098.html
https://blog.csdn.net/moshowgame/article/details/81016482
https://www.reinforce.cn/t/619.html