The JSON format field is a new attribute added in MySQL 5.7, but it is essentially saved in the database as a string. When I first came into contact with it, I only knew the $.xx method of querying fields, because most of the time, this is enough, and the rest can be left to the program. However, some recent operations require more complex query operations, so I quickly learned more methods. JSON_EXTRACT(json_doc [,path]) Query Fields mysql> set @j = '{"name":"wxnacy"}'; mysql> select JSON_EXTRACT(@j, '$.name'); +----------------------------+ | JSON_EXTRACT(@j, '$.name') | +----------------------------+ | "wxnacy" | +----------------------------+ There is also a more concise way, but it can only be used when querying the table mysql> select ext -> '$.name' from test; +-----------------+ | ext -> '$.name' | +-----------------+ | "wxnacy" | +-----------------+ After $., you can use the JSON format to obtain data, such as arrays. mysql> set @j = '{"a": [1, 2]}'; mysql> select JSON_EXTRACT(@j, '$.a[0]'); +----------------------------+ | JSON_EXTRACT(@j, '$.a[0]') | +----------------------------+ | 1 | +----------------------------+ JSON_DEPTH(json_doc) Calculate the depth of JSON. The calculation method is {} []. If there is a symbol, it is a layer. If there is data under the symbol, an additional layer is added. Complex JSON is calculated until the deepest one. The official document says that the null value has a depth of 0, but the actual effect is not the case. Here are a few examples JSON_LENGTH(json_doc [, path]) Calculates the length of the outermost JSON or the specified path. The scalar length is 1. The length of an array is the number of array elements, and the length of an object is the number of object members. mysql> SELECT JSON_LENGTH('[1, 2, {"a": 3}]'); +---------------------------------+ | JSON_LENGTH('[1, 2, {"a": 3}]') | +---------------------------------+ | 3 | +---------------------------------+ mysql> SELECT JSON_LENGTH('{"a": 1, "b": {"c": 30}}'); +-----------------------------------------+ | JSON_LENGTH('{"a": 1, "b": {"c": 30}}') | +-----------------------------------------+ | 2 | +-----------------------------------------+ mysql> SELECT JSON_LENGTH('{"a": 1, "b": {"c": 30}}', '$.b'); +------------------------------------------------+ | JSON_LENGTH('{"a": 1, "b": {"c": 30}}', '$.b') | +------------------------------------------------+ | 1 | +------------------------------------------------+ JSON_TYPE(json_doc) Returns a utf8mb4 string indicating the type of the JSON value. This can be an object, array, or scalar type, as shown below: mysql> SET @j = '{"a": [10, true]}'; mysql> SELECT JSON_TYPE(@j); +---------------+ | JSON_TYPE(@j) | +---------------+ | OBJECT | +---------------+ mysql> SELECT JSON_TYPE(JSON_EXTRACT(@j, '$.a')); +------------------------------------+ | JSON_TYPE(JSON_EXTRACT(@j, '$.a')) | +------------------------------------+ | ARRAY | +------------------------------------+ mysql> SELECT JSON_TYPE(JSON_EXTRACT(@j, '$.a[0]')); +---------------------------------------+ | JSON_TYPE(JSON_EXTRACT(@j, '$.a[0]')) | +---------------------------------------+ | INTEGER | +---------------------------------------+ mysql> SELECT JSON_TYPE(JSON_EXTRACT(@j, '$.a[1]')); +---------------------------------------+ | JSON_TYPE(JSON_EXTRACT(@j, '$.a[1]')) | +---------------------------------------+ | BOOLEAN | +---------------------------------------+ Possible return types Pure JSON type:
Number Type:
Time Type:
String type: STRING: MySQL utf8 character type scalars: CHAR, VARCHAR, TEXT, ENUM, and SET Binary Type: BLOB: MySQL binary type scalars, including BINARY, VARBINARY, BLOB, and BIT All other types: OPAQUE (raw bit) JSON_VALID Returns 0 or 1 to indicate whether the value is valid JSON. If the argument is NULL, returns NULL. mysql> SELECT JSON_VALID('{"a": 1}'); +------------------------+ | JSON_VALID('{"a": 1}') | +------------------------+ | 1 | +------------------------+ mysql> SELECT JSON_VALID('hello'), JSON_VALID('"hello"'); +---------------------+-----------------------+ | JSON_VALID('hello') | JSON_VALID('"hello"') | +---------------------+-----------------------+ | 0 | 1 | +---------------------+-----------------------+ The above is the detailed content of the summary of related functions for Mysql query JSON results. For more information about MySQL json functions, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
>>: Solve the problem of multiple listeners reported when starting tomcat in Idea
Table of contents 1. Phenomenon 2. Solution 3. Su...
The earliest computers could only use ASCII chara...
Table of contents 1. Reverse proxy preparation 1....
When executing yum in dockerfile or in the contai...
1. Download MySQL Log in to the MySQL official we...
First, you need to determine which fields or fiel...
I encountered this problem before when developing...
In daily work, we sometimes run slow queries to r...
1. Purchase of Server 1. I chose Alibaba Cloud...
The link-in style is to put all the styles in one...
1. Table structure TABLE person id name 1 you 2 Y...
Robots.txt is a plain text file in which website ...
The configuration is very simple, but I have to c...
Table of contents 1. Overview 1.1 What is a proto...
Simply put, delayed replication is to set a fixed...