MySQL处理JSON常见函数的使用
官方文档:json functions
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 |
1. 概述
mysql里的json分为json array和json object。 $表示整个json对象,在索引数据时用下标(对于json array,从0开始)或键值(对于json object,含有特殊字符的key要用"括起来,比如$."my name")。
例如:[3, {"a": [5, 6], "b": 10}, [99, 100]],那么:
$[0]:3
$[1]: {"a": [5, 6], "b": 10}
$[2] :[99, 100]
$[3] : null
$[1].a:[5, 6]
$[1].a[1]:6
$[1].b:10
$[2][0]:99
2. 比较规则
json中的数据可以用 =, <, <=, >, >=, <>, !=, and <=> 进行比较。但json里的数据类型可以是多样的,那么在不同类型之间进行比较时,就有优先级了,高优先级的要大于低优先级的
(可以用json_type()函数查看类型)。优先级从高到低如下:
blob
bit
opaque
datetime
time
date
boolean
array
object
string
integer, double
null
3. 常用函数
3.1 创建函数
3.1.1 json_array
json_array(val1,val2,val3...)
生成一个包含指定元素的json数组。
mysql> select json_array(1, "abc", null, true, curtime()); +---------------------------------------------+ | json_array(1, "abc", null, true, curtime()) | +---------------------------------------------+ | [1, "abc", null, true, "11:30:24.000000"] | +---------------------------------------------+
3.1.2 json_object
json_object(key1,val1,key2,val2...)
生成一个包含指定k-v对的json object。如果有key为null或参数个数为奇数,则抛错。
mysql> select json_object('id', 87, 'name', 'carrot'); +-----------------------------------------+ | json_object('id', 87, 'name', 'carrot') | +-----------------------------------------+ | {"id": 87, "name": "carrot"} | +-----------------------------------------+
3.1.3 json_quote
json_quote(json_val)
将json_val用"号括起来。
mysql> select json_quote('null'), json_quote('"null"'); +--------------------+----------------------+ | json_quote('null') | json_quote('"null"') | +--------------------+----------------------+ | "null" | "\"null\"" | +--------------------+----------------------+ mysql> select json_quote('[1, 2, 3]'); +-------------------------+ | json_quote('[1, 2, 3]') | +-------------------------+ | "[1, 2, 3]" | +-------------------------+
3.1.4 convert
convert(json_string,json)
mysql> select convert('{"mail": "amy@gmail.com", "name": "amy"}',json); +----------------------------------------------------------+ | convert('{"mail": "amy@gmail.com", "name": "amy"}',json) | +----------------------------------------------------------+ | {"mail": "amy@gmail.com", "name": "amy"} | +----------------------------------------------------------+
3.2 查询函数
3.2.1 json_contains
json_contains(json_doc, val[, path])
查询json文档是否在指定path包含指定的数据,包含则返回1,否则返回0。如果有参数为null或path不存在,则返回null。
mysql> set @j = '{"a": 1, "b": 2, "c": {"d": 4}}'; mysql> set @j2 = '1'; mysql> select json_contains(@j, @j2, '$.a'); +-------------------------------+ | json_contains(@j, @j2, '$.a') | +-------------------------------+ | 1 | +-------------------------------+ mysql> select json_contains(@j, @j2, '$.b'); +-------------------------------+ | json_contains(@j, @j2, '$.b') | +-------------------------------+ | 0 | +-------------------------------+ mysql> set @j2 = '{"d": 4}'; mysql> select json_contains(@j, @j2, '$.a'); +-------------------------------+ | json_contains(@j, @j2, '$.a') | +-------------------------------+ | 0 | +-------------------------------+ mysql> select json_contains(@j, @j2, '$.c'); +-------------------------------+ | json_contains(@j, @j2, '$.c') | +-------------------------------+ | 1 | +-------------------------------+
3.2.2 json_contains_path
json_contains_path(json_doc, one_or_all, path[, path] ...)
查询是否存在指定路径,存在则返回1,否则返回0。如果有参数为null,则返回null。
one_or_all只能取值"one"或"all",one表示只要有一个存在即可;all表示所有的都存在才行。
mysql> set @j = '{"a": 1, "b": 2, "c": {"d": 4}}'; mysql> select json_contains_path(@j, 'one', '$.a', '$.e'); +---------------------------------------------+ | json_contains_path(@j, 'one', '$.a', '$.e') | +---------------------------------------------+ | 1 | +---------------------------------------------+ mysql> select json_contains_path(@j, 'all', '$.a', '$.e'); +---------------------------------------------+ | json_contains_path(@j, 'all', '$.a', '$.e') | +---------------------------------------------+ | 0 | +---------------------------------------------+ mysql> select json_contains_path(@j, 'one', '$.c.d'); +----------------------------------------+ | json_contains_path(@j, 'one', '$.c.d') | +----------------------------------------+ | 1 | +----------------------------------------+ mysql> select json_contains_path(@j, 'one', '$.a.d'); +----------------------------------------+ | json_contains_path(@j, 'one', '$.a.d') | +----------------------------------------+ | 0 | +----------------------------------------+
3.2.3 json_extract
json_extract(json_doc, path[, path] ...)
从json文档里抽取数据。如果有参数有null或path不存在,则返回null。如果抽取出多个path,则返回的数据封闭在一个json array里。
mysql> select json_extract('[10, 20, [30, 40]]', '$[1]'); +--------------------------------------------+ | json_extract('[10, 20, [30, 40]]', '$[1]') | +--------------------------------------------+ | 20 | +--------------------------------------------+ mysql> select json_extract('[10, 20, [30, 40]]', '$[1]', '$[0]'); +----------------------------------------------------+ | json_extract('[10, 20, [30, 40]]', '$[1]', '$[0]') | +----------------------------------------------------+ | [20, 10] | +----------------------------------------------------+ mysql> select json_extract('[10, 20, [30, 40]]', '$[2][*]'); +-----------------------------------------------+ | json_extract('[10, 20, [30, 40]]', '$[2][*]') | +-----------------------------------------------+ | [30, 40] | +-----------------------------------------------+
在mysql 5.7.9+里可以用"->"替代。
mysql> select c, json_extract(c, "$.id"), g > from jemp > where json_extract(c, "$.id") > 1 > order by json_extract(c, "$.name"); +-------------------------------+-----------+------+ | c | c->"$.id" | g | +-------------------------------+-----------+------+ | {"id": "3", "name": "barney"} | "3" | 3 | | {"id": "4", "name": "betty"} | "4" | 4 | | {"id": "2", "name": "wilma"} | "2" | 2 | +-------------------------------+-----------+------+ 3 rows in set (0.00 sec) mysql> select c, c->"$.id", g > from jemp > where c->"$.id" > 1 > order by c->"$.name"; +-------------------------------+-----------+------+ | c | c->"$.id" | g | +-------------------------------+-----------+------+ | {"id": "3", "name": "barney"} | "3" | 3 | | {"id": "4", "name": "betty"} | "4" | 4 | | {"id": "2", "name": "wilma"} | "2" | 2 | +-------------------------------+-----------+------+ 3 rows in set (0.00 sec)
在mysql 5.7.13+,还可以用"->>"表示去掉抽取结果的"号,下面三种效果是一样的:
- json_unquote( json_extract(column, path) )
- json_unquote(column -> path)
- column->>path
mysql> select * from jemp where g > 2; +-------------------------------+------+ | c | g | +-------------------------------+------+ | {"id": "3", "name": "barney"} | 3 | | {"id": "4", "name": "betty"} | 4 | +-------------------------------+------+ 2 rows in set (0.01 sec) mysql> select c->'$.name' as name -> from jemp where g > 2; +----------+ | name | +----------+ | "barney" | | "betty" | +----------+ 2 rows in set (0.00 sec) mysql> select json_unquote(c->'$.name') as name -> from jemp where g > 2; +--------+ | name | +--------+ | barney | | betty | +--------+ 2 rows in set (0.00 sec) mysql> select c->>'$.name' as name -> from jemp where g > 2; +--------+ | name | +--------+ | barney | | betty | +--------+ 2 rows in set (0.00 sec)
3.2.4 json_keys
json_keys(json_doc[, path])
获取json文档在指定路径下的所有键值,返回一个json array。如果有参数为null或path不存在,则返回null。
mysql> select json_keys('{"a": 1, "b": {"c": 30}}'); +---------------------------------------+ | json_keys('{"a": 1, "b": {"c": 30}}') | +---------------------------------------+ | ["a", "b"] | +---------------------------------------+ mysql> select json_keys('{"a": 1, "b": {"c": 30}}', '$.b'); +----------------------------------------------+ | json_keys('{"a": 1, "b": {"c": 30}}', '$.b') | +----------------------------------------------+ | ["c"] | +----------------------------------------------+
3.2.5 json_search
json_search(json_doc, one_or_all, search_str[, escape_char[, path] ...])
查询包含指定字符串的paths,并作为一个json array返回。如果有参数为nul或path不存在,则返回null。
- one_or_all:"one"表示查询到一个即返回;"all"表示查询所有。
- search_str:要查询的字符串。 可以用like里的'%'或‘_'匹配。
- path:在指定path下查。
mysql> set @j = '["abc", [{"k": "10"}, "def"], {"x":"abc"}, {"y":"bcd"}]'; mysql> select json_search(@j, 'one', 'abc'); +-------------------------------+ | json_search(@j, 'one', 'abc') | +-------------------------------+ | "$[0]" | +-------------------------------+ mysql> select json_search(@j, 'all', 'abc'); +-------------------------------+ | json_search(@j, 'all', 'abc') | +-------------------------------+ | ["$[0]", "$[2].x"] | +-------------------------------+ mysql> select json_search(@j, 'all', 'ghi'); +-------------------------------+ | json_search(@j, 'all', 'ghi') | +-------------------------------+ | null | +-------------------------------+ mysql> select json_search(@j, 'all', '10'); +------------------------------+ | json_search(@j, 'all', '10') | +------------------------------+ | "$[1][0].k" | +------------------------------+ mysql> select json_search(@j, 'all', '10', null, '$'); +-----------------------------------------+ | json_search(@j, 'all', '10', null, '$') | +-----------------------------------------+ | "$[1][0].k" | +-----------------------------------------+ mysql> select json_search(@j, 'all', '10', null, '$[*]'); +--------------------------------------------+ | json_search(@j, 'all', '10', null, '$[*]') | +--------------------------------------------+ | "$[1][0].k" | +--------------------------------------------+ mysql> select json_search(@j, 'all', '10', null, '$**.k'); +---------------------------------------------+ | json_search(@j, 'all', '10', null, '$**.k') | +---------------------------------------------+ | "$[1][0].k" | +---------------------------------------------+ mysql> select json_search(@j, 'all', '10', null, '$[*][0].k'); +-------------------------------------------------+ | json_search(@j, 'all', '10', null, '$[*][0].k') | +-------------------------------------------------+ | "$[1][0].k" | +-------------------------------------------------+ mysql> select json_search(@j, 'all', '10', null, '$[1]'); +--------------------------------------------+ | json_search(@j, 'all', '10', null, '$[1]') | +--------------------------------------------+ | "$[1][0].k" | +--------------------------------------------+ mysql> select json_search(@j, 'all', '10', null, '$[1][0]'); +-----------------------------------------------+ | json_search(@j, 'all', '10', null, '$[1][0]') | +-----------------------------------------------+ | "$[1][0].k" | +-----------------------------------------------+ mysql> select json_search(@j, 'all', 'abc', null, '$[2]'); +---------------------------------------------+ | json_search(@j, 'all', 'abc', null, '$[2]') | +---------------------------------------------+ | "$[2].x" | +---------------------------------------------+ mysql> select json_search(@j, 'all', '%a%'); +-------------------------------+ | json_search(@j, 'all', '%a%') | +-------------------------------+ | ["$[0]", "$[2].x"] | +-------------------------------+ mysql> select json_search(@j, 'all', '%b%'); +-------------------------------+ | json_search(@j, 'all', '%b%') | +-------------------------------+ | ["$[0]", "$[2].x", "$[3].y"] | +-------------------------------+ mysql> select json_search(@j, 'all', '%b%', null, '$[0]'); +---------------------------------------------+ | json_search(@j, 'all', '%b%', null, '$[0]') | +---------------------------------------------+ | "$[0]" | +---------------------------------------------+ mysql> select json_search(@j, 'all', '%b%', null, '$[2]'); +---------------------------------------------+ | json_search(@j, 'all', '%b%', null, '$[2]') | +---------------------------------------------+ | "$[2].x" | +---------------------------------------------+ mysql> select json_search(@j, 'all', '%b%', null, '$[1]'); +---------------------------------------------+ | json_search(@j, 'all', '%b%', null, '$[1]') | +---------------------------------------------+ | null | +---------------------------------------------+ mysql> select json_search(@j, 'all', '%b%', '', '$[1]'); +-------------------------------------------+ | json_search(@j, 'all', '%b%', '', '$[1]') | +-------------------------------------------+ | null | +-------------------------------------------+ mysql> select json_search(@j, 'all', '%b%', '', '$[3]'); +-------------------------------------------+ | json_search(@j, 'all', '%b%', '', '$[3]') | +-------------------------------------------+ | "$[3].y" | +-------------------------------------------+
3.3 修改函数
3.3.1 json_append/json_array_append
json_array_append(json_doc, path, val[, path, val] ...)
在指定path的json array尾部追加val。如果指定path是一个json object,则将其封装成一个json array再追加。如果有参数为null,则返回null。
mysql> set @j = '["a", ["b", "c"], "d"]'; mysql> select json_array_append(@j, '$[1]', 1); +----------------------------------+ | json_array_append(@j, '$[1]', 1) | +----------------------------------+ | ["a", ["b", "c", 1], "d"] | +----------------------------------+ mysql> select json_array_append(@j, '$[0]', 2); +----------------------------------+ | json_array_append(@j, '$[0]', 2) | +----------------------------------+ | [["a", 2], ["b", "c"], "d"] | +----------------------------------+ mysql> select json_array_append(@j, '$[1][0]', 3); +-------------------------------------+ | json_array_append(@j, '$[1][0]', 3) | +-------------------------------------+ | ["a", [["b", 3], "c"], "d"] | +-------------------------------------+ mysql> set @j = '{"a": 1, "b": [2, 3], "c": 4}'; mysql> select json_array_append(@j, '$.b', 'x'); +------------------------------------+ | json_array_append(@j, '$.b', 'x') | +------------------------------------+ | {"a": 1, "b": [2, 3, "x"], "c": 4} | +------------------------------------+ mysql> select json_array_append(@j, '$.c', 'y'); +--------------------------------------+ | json_array_append(@j, '$.c', 'y') | +--------------------------------------+ | {"a": 1, "b": [2, 3], "c": [4, "y"]} | +--------------------------------------+ mysql> set @j = '{"a": 1}'; mysql> select json_array_append(@j, '$', 'z'); +---------------------------------+ | json_array_append(@j, '$', 'z') | +---------------------------------+ | [{"a": 1}, "z"] | +---------------------------------+
3.3.2 json_array_insert
json_array_insert(json_doc, path, val[, path, val] ...)
在path指定的json array元素插入val,原位置及以右的元素顺次右移。如果path指定的数据非json array元素,则略过此val;如果指定的元素下标超过json array的长度,则插入尾部。
mysql> set @j = '["a", {"b": [1, 2]}, [3, 4]]'; mysql> select json_array_insert(@j, '$[1]', 'x'); +------------------------------------+ | json_array_insert(@j, '$[1]', 'x') | +------------------------------------+ | ["a", "x", {"b": [1, 2]}, [3, 4]] | +------------------------------------+ mysql> select json_array_insert(@j, '$[100]', 'x'); +--------------------------------------+ | json_array_insert(@j, '$[100]', 'x') | +--------------------------------------+ | ["a", {"b": [1, 2]}, [3, 4], "x"] | +--------------------------------------+ mysql> select json_array_insert(@j, '$[1].b[0]', 'x'); +-----------------------------------------+ | json_array_insert(@j, '$[1].b[0]', 'x') | +-----------------------------------------+ | ["a", {"b": ["x", 1, 2]}, [3, 4]] | +-----------------------------------------+ mysql> select json_array_insert(@j, '$[2][1]', 'y'); +---------------------------------------+ | json_array_insert(@j, '$[2][1]', 'y') | +---------------------------------------+ | ["a", {"b": [1, 2]}, [3, "y", 4]] | +---------------------------------------+ mysql> select json_array_insert(@j, '$[0]', 'x', '$[2][1]', 'y'); +----------------------------------------------------+ | json_array_insert(@j, '$[0]', 'x', '$[2][1]', 'y') | +----------------------------------------------------+ | ["x", "a", {"b": [1, 2]}, [3, 4]] | +----------------------------------------------------+
3.3.3 json_insert/json_replace/json_set
json_insert(json_doc, path, val[, path, val] ...)
在指定path下插入数据,如果path已存在,则忽略此val(不存在才插入)。
mysql> set @j = '{ "a": 1, "b": [2, 3]}'; mysql> select json_insert(@j, '$.a', 10, '$.c', '[true, false]'); +----------------------------------------------------+ | json_insert(@j, '$.a', 10, '$.c', '[true, false]') | +----------------------------------------------------+ | {"a": 1, "b": [2, 3], "c": "[true, false]"} | +----------------------------------------------------+
json_replace(json_doc, path, val[, path, val] ...)
替换指定路径的数据,如果某个路径不存在则略过(存在才替换)。如果有参数为null,则返回null。
mysql> set @j = '{ "a": 1, "b": [2, 3]}'; mysql> select json_replace(@j, '$.a', 10, '$.c', '[true, false]'); +-----------------------------------------------------+ | json_replace(@j, '$.a', 10, '$.c', '[true, false]') | +-----------------------------------------------------+ | {"a": 10, "b": [2, 3]} | +-----------------------------------------------------+
json_set(json_doc, path, val[, path, val] ...)
设置指定路径的数据(不管是否存在)。如果有参数为null,则返回null。
mysql> set @j = '{ "a": 1, "b": [2, 3]}'; mysql> select json_set(@j, '$.a', 10, '$.c', '[true, false]'); +-------------------------------------------------+ | json_set(@j, '$.a', 10, '$.c', '[true, false]') | +-------------------------------------------------+ | {"a": 10, "b": [2, 3], "c": "[true, false]"} | +-------------------------------------------------+ mysql> select json_insert(@j, '$.a', 10, '$.c', '[true, false]'); +----------------------------------------------------+ | json_insert(@j, '$.a', 10, '$.c', '[true, false]') | +----------------------------------------------------+ | {"a": 1, "b": [2, 3], "c": "[true, false]"} | +----------------------------------------------------+ mysql> select json_replace(@j, '$.a', 10, '$.c', '[true, false]'); +-----------------------------------------------------+ | json_replace(@j, '$.a', 10, '$.c', '[true, false]') | +-----------------------------------------------------+ | {"a": 10, "b": [2, 3]} | +-----------------------------------------------------+
3.3.4 json_merge
json_merge(json_doc, json_doc[, json_doc] ...)
merge多个json文档。规则如下:
- 如果都是json array,则结果自动merge为一个json array;
- 如果都是json object,则结果自动merge为一个json object;
- 如果有多种类型,则将非json array的元素封装成json array再按照规则一进行mege。
mysql> select json_merge('[1, 2]', '[true, false]'); +---------------------------------------+ | json_merge('[1, 2]', '[true, false]') | +---------------------------------------+ | [1, 2, true, false] | +---------------------------------------+ mysql> select json_merge('{"name": "x"}', '{"id": 47}'); +-------------------------------------------+ | json_merge('{"name": "x"}', '{"id": 47}') | +-------------------------------------------+ | {"id": 47, "name": "x"} | +-------------------------------------------+ mysql> select json_merge('1', 'true'); +-------------------------+ | json_merge('1', 'true') | +-------------------------+ | [1, true] | +-------------------------+ mysql> select json_merge('[1, 2]', '{"id": 47}'); +------------------------------------+ | json_merge('[1, 2]', '{"id": 47}') | +------------------------------------+ | [1, 2, {"id": 47}] | +------------------------------------+
3.3.5 json_remove
json_remove(json_doc, path[, path] ...)
移除指定路径的数据,如果某个路径不存在则略过此路径。如果有参数为null,则返回null。
mysql> set @j = '["a", ["b", "c"], "d"]'; mysql> select json_remove(@j, '$[1]'); +-------------------------+ | json_remove(@j, '$[1]') | +-------------------------+ | ["a", "d"] | +-------------------------+
3.3.6 json_unquote
json_unquote(val)
去掉val的引号。如果val为null,则返回null。
mysql> set @j = '"abc"'; mysql> select @j, json_unquote(@j); +-------+------------------+ | @j | json_unquote(@j) | +-------+------------------+ | "abc" | abc | +-------+------------------+ mysql> set @j = '[1, 2, 3]'; mysql> select @j, json_unquote(@j); +-----------+------------------+ | @j | json_unquote(@j) | +-----------+------------------+ | [1, 2, 3] | [1, 2, 3] | +-----------+------------------+
3.4 json特性查询
3.4.1 json_deepth
json_depth(json_doc)
获取json文档的深度。如果参数为null,则返回null。
空的json array、json object或标量的深度为1。
mysql> select json_depth('{}'), json_depth('[]'), json_depth('true'); +------------------+------------------+--------------------+ | json_depth('{}') | json_depth('[]') | json_depth('true') | +------------------+------------------+--------------------+ | 1 | 1 | 1 | +------------------+------------------+--------------------+ mysql> select json_depth('[10, 20]'), json_depth('[[], {}]'); +------------------------+------------------------+ | json_depth('[10, 20]') | json_depth('[[], {}]') | +------------------------+------------------------+ | 2 | 2 | +------------------------+------------------------+ mysql> select json_depth('[10, {"a": 20}]'); +-------------------------------+ | json_depth('[10, {"a": 20}]') | +-------------------------------+ | 3 | +-------------------------------+
3.4.2 json_length
json_length(json_doc[, path])
获取指定路径下的长度。如果参数为null,则返回null。
长度的计算规则:
- 标量的长度为1;
- json array的长度为元素的个数;
- json object的长度为key的个数。
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 | +------------------------------------------------+
3.4.3 json_type
json_type(json_val)
获取json文档的具体类型。如果参数为null,则返回null。
3.4.4 json_valid
json_valid(val)
判断val是否为有效的json格式,是为1,不是为0。如果参数为nul,则返回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 | +---------------------+-----------------------+
到此这篇关于mysql处理json常见函数的使用的文章就介绍到这了,更多相关mysql json常见函数内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!