MySql json字段操作函数

分类

函数

描述

创建json

json_array

创建json数组

 

json_object

创建json对象

 

json_quote

将json转成json字符串类型

查询json

json_contains

判断是否包含某个json值

 

json_contains_path

判断某个路径下是否包json值

 

json_extract

提取json值

 

column->path

json_extract的简洁写法,MySQL 5.7.9开始支持

 

column->>path

json_unquote(column -> path)的简洁写法

 

json_keys

提取json中的键值为json数组

 

json_search

按给定字符串关键字搜索json,返回匹配的路径

修改json

json_append

废弃,MySQL 5.7.9开始改名为json_array_append

 

json_array_append

末尾添加数组元素,如果原有值是数值或json对象,则转成数组后,再添加元素

 

json_array_insert

插入数组元素

 

json_insert

插入值(插入新值,但不替换已经存在的旧值)

 

json_merge

合并json数组或对象

 

json_remove

删除json数据

 

json_replace

替换值(只替换已经存在的旧值)

 

json_set

设置值(替换旧值,并插入不存在的新值)

 

json_unquote

去除json字符串的引号,将值转成string类型

返回json属性

json_depth

返回json文档的最大深度

 

json_length

返回json文档的长度

 

json_type

返回json值得类型

 

json_valid

判断是否为合法json文档

一、查询json字段中某一个对象

 

SELECT id,jsontest,jsontest->'$.row' as row1,jsontest->'$.celldata' as celldata FROM `test`

 

mysql json便利 mysql json处理_数组

 字符串返回的不同

SELECT id,jsontest,jsontest->'$.name' as name1, jsontest->>'$.name' as name2 FROM `test` where id=1

mysql json便利 mysql json处理_JSON_02

查询时,不返回指定字段

 

mysql json便利 mysql json处理_JSON_03

SELECT id,json_remove(jsontest,"$.cell") from test where id=2;

mysql json便利 mysql json处理_数组_04

二、更新json

update test set jsontest=json_set(jsontest,"$.x","vv")  where id=2;

mysql json便利 mysql json处理_数组_05

update test set jsontest=json_set(jsontest,"$.x.z","bb")  where id=2;

mysql json便利 mysql json处理_json_06

替换整个对象

update test set jsontest=json_set(jsontest,"$.x",JSON_OBJECT("a","a1","b","b1"))  where id=2;

mysql json便利 mysql json处理_数组_07

更新多个key

update test set jsontest=json_set(jsontest,"$.k2","1"), jsontest=json_set(jsontest,"$.k","2")  where id=2;

mysql json便利 mysql json处理_json_08

替换数组

update test set jsontest=json_set(jsontest,"$.x",JSON_Array("a1","a2","a3"))  where id=2;

mysql json便利 mysql json处理_json_09

插入数据组(对象为json字符串)

update test set jsontest=json_array_append(jsontest,"$.cell",CAST('{"r": 1, "v": "v1"}' as JSON))  where id=2;

mysql json便利 mysql json处理_数组_10

 

三、删除json

update test set jsontest=json_remove(jsontest,"$.x.a")  where id=2;

mysql json便利 mysql json处理_数组_11

update test set jsontest=json_remove(jsontest,"$.x")  where id=2;

mysql json便利 mysql json处理_json_12

四、数组处理

示例:{"k": "vv", "cell": [{"r": 1, "v": "v1"}]}

update test set jsontest=json_array_append(jsontest,"$.cell",json_object("r",1, "v", "v1"))  where id=2;

mysql json便利 mysql json处理_json_13

插入到数组0

update test set jsontest=json_array_insert(jsontest,"$.cell[0]",json_object("r",2, "v", "v2"))  where id=2;

mysql json便利 mysql json处理_JSON_14

去除位置为1的

update test set jsontest=json_remove(jsontest,"$.cell[1]")  where id=2;

mysql json便利 mysql json处理_json_15

更新集合位置x的对象中key的值 

mysql json便利 mysql json处理_数组_16

update test set jsontest=json_set(jsontest,"$.cell2[1].r",CAST('{"ct":"test","v":1,"m":"1222"}' as JSON))  where id=2;

mysql json便利 mysql json处理_JSON_17

 

 

 

 

四、CAST使用

更新中

update test t set t.jsontest=json_set(t.jsontest,"$.x",CAST('{"a":"b"}' as JSON))  where id=2;

mysql json便利 mysql json处理_数组_18

 

 

查询中

SELECT t.jsontest->>'$.cell', CAST(t.jsontest->>'$.cell' AS JSON) from test t where t.id=2;

mysql json便利 mysql json处理_json_19