mysql的json类型的所有函数
⽰例表
后⾯的所有的表demo_json结构都是这个
create table demo_json(
`id`bigint unsigned not null auto_increment,
`json` json not null,
primary key(`id`)
)engine=innodb;
json_set
⽤于将对应的json已有的字段进⾏修改
语法
json_set(json_doc, path, value, [path, value, …])
说明:
其中json_doc就是表中对应的json列,path就是json中对应的字段key,value就是对应的值,后⾯的都是这样。返回值就是修改后的值
-- 插⼊数据
insert into demo_json(`json`)values('{"ok":12}');
-- 更新数据
update demo_json set`json`=json_set(`json`,'$.f1',2333,'$.f2',"v1");
-- 选择数据
select`json`from demo_json;
-- 返回
{"f1": 2333,"f2": "v1","ok": 12}
json_keys
返回对应⽂档中的最上层的keys,如果内部还有更多嵌套的key,则是不会嵌套返回的
json_keys(json_doc[, path])
-- ⽆path参数,返回的是json_doc中的顶级key
-- 返回[a,b]
select json_kesy('{"a":12, "b":32}');
-- 返回[a, b, c]
select json_keys('{"a":12, "b":32, "c":{"ok":11, "kk":43}}');
-- 有path参数,则返回的是path对应⽂档中的顶级key
-- 返回[ok, kk]
select json_keys('{"a":12, "b":32, "c":{"ok":11, "kk":43}}','$.c');
json_type
返回对应⽂档中的字段值的类型
json_type(value)
普通情况下直接返回,但是如果⽂档的话,可以⽤函数json_extract(json_doc, path[, path…]) 进⾏提取即可
-- 返回integer
select json_type('12');
-- 返回double
select json_type('12.0');
-- 返回string
select json_type('"abc"');
-- 返回object
select json_type('{"a":12,"b":"v1"}');
-- 返回array
select json_type('["a", 1]');
-- 返回 {"a": 12, "b": "vv"}
select`json`from demo_json where id =3;
-- 返回integer
select json_type(json_extract(`json`,'$.a'))from demo_json where id =3;
-- 返回string
select json_type(json_extract(`json`,'$.b'))from demo_json where id =3;
json_array
该函数⽤于将数据进⾏拼接,其实就有点像java中的new ArrayList() 这种json_array(value[,value…])
-
- 返回数组:["a", "1", 34]
select json_array('a','1',34);
json_depth
返回⽂档的深度
json_depth(json_doc)
-- 返回1
select json_depth('[]');
-- 返回1
select json_depth('{}');
-- 返回1
select json_depth('12');
-
- 返回1
select json_depth('"a"');
-- 返回2
select json_depth('[1]');
-- 返回2
select json_depth('[1, 2, 3, "a"]');
-- 返回2
select json_depth('{"a":12}');
-- 返回2
select json_depth('{"a":12, "b":"v"}');
-- 返回3
select json_depth('["a", {"b":12}]');
-- 返回3
select json_depth('[{"a":10}, {"b":12}]');
-- 返回3
select json_depth('{"a":12, "b":{"b1":12}}');
其中普通的空以及普通字段,深度是1级,⼆级的话,就是普通的数组和对象json_quote
将⾮json_doc⽂档格式的数据,转换为⽂档格式
json_quote(string)
-- 返回 ""
select json_quote('a');
-- 返回 "\"a\""
select json_quote('"a"');
-- 返回 ""
select json_quote('');
-- 返回 "[a, b]"
select json_quote('[a, b]');
-- 返回 "[\"a\", \"b\"]"
select json_quote('["a", "b"]');
json_valid
判断值是否是json类型
json_valid(val)
-- 返回 null
select json_valid(null);
-- 返回 0
select json_valid('');
-- 返回 0
select json_valid('a');
-- 返回 1
select json_valid('[1,2]');
-- 返回 0
select json_valid('{a,1}');
-- 返回 1
select json_valid('{"a":12, "b":2}');
json_insert
给对应的⽂档添加数据,这个给update的时候,这样设置,更⽅便select json_insert(json_doc, path, val[, path, val] …)
-- 插⼊数据
insert into demo_json(`json`)values('{"a":1, "b":2}');
-- {"a": 1, "b": 2}
select`json`from demo_json;
update demo_json set`json`=json_insert(`json`,'$.c','3');
-- {"a": 1, "b": 2, "c": "3"}
select`json`from demo_json;
json_length
返回对应⽂档的长度,我们知道⽂档有这么⼏种类型:标量、对象、数组
⽂件长度确定如下:
标量的长度为1。
数组的长度是数组元素的数量。
对象的长度是对象成员的数量。
该长度不计算嵌套数组或对象的长度。
-- 返回错误
select json_length('a');
select json_length(1);
select json_length('');
-- 0
select json_length('{}');
select json_length('[]');
select json_length('null');
-- 1
select json_length('"2"');
select json_length('[1]');
select json_length('{"a":1}');
-- 2
select json_length('[1, "a"]');
select json_length('{"a":1, "b":2}');json值的类型有哪些
--------- ⽂档类型 ---------
truncate demo_json;
insert into demo_json(`json`)values('{"a": 1, "b": 2, "c": {"c1":1, "c2": 2}}');
-- {"a": 1, "b": 2, "c": {"c1":1, "c2": 2}}
select`json`from demo_json;
-- 1
select json_length(`json`,'$.a')from demo_json where id =1;
-- 2
select json_length(`json`,'$.c')from demo_json where id =1;
-- 3
select json_length(`json`)from demo_json where id =1;
json_object
其实就是把⼀些值转换为object格式,跟函数json_array有点相同json_object([key, val[, key, val] …])
json_pretty
该函数就是把⽂档给打印出来,按照json格式进⾏打印
-- 1
select json_pretty('1');
-- "a"
select json_pretty('"a"');
-- 返回
-- [
--  1,
--  2,
--  "a"
-
- ]
select json_pretty('[1, 2, "a"]');
-- 返回
-- {
--  "a": 1,
--  "b": 12,
--  "c": 39
-- }
select json_pretty('{"a":1, "b":12, "c":39}');
json_remove
从⽂档中删除指定的元素,然后返回
select json_remove(json_doc, path[, path] …)
-- {"b": 2, "c": 3}
select json_remove('{"a":1, "b":2, "c":3}','$.a');
-- {"c": 3}
select json_remove('{"a":1, "b":2, "c":3}','$.a','$.b');
-- [3, 2]
select json_remove('[12, 3, 2]','$[0]');
-- [12, 2]
select json_remove('[12, 3, 2]','$[1]');
------- 使⽤在字段上 ------
truncate demo_json;
insert into demo_json(`json`)values('{"a":12, "b":2}');
update demo_json set`json`=json_remove(`json`,'$.a')where id =1;
-- {"b": 2}
select`json`from demo_json;
json_search
该函数返回的是指定字符串的路径,就是doc中的字段
json_search(json_doc, one_or_all, search_str[, escape_char[, path] …])说明:
one_or_all:
‘one’:搜索到⼀个就直接返回
‘all’:搜索到所有的才返回,所有的字段会包装成⼀个数组