select
json_extract('{"name":"zhangsan","tel_no":"136-6666-6666","hobbies":["basketball","run","sing"]}',"$.name") as name,
json_extract('{"name":"zhangsan","tel_no":"136-6666-6666","hobbies":["basketball","run","sing"]}',"$.tel_no") as tel_no,
json_extract('{"name":"zhangsan","tel_no":"136-6666-6666","hobbies":["basketball","run","sing"]}',"$.hobbies[0]") as hobby_1,
json_extract('{"name":"zhangsan","tel_no":"136-6666-6666","hobbies":["basketball","run","sing"]}',"$.hobbies[1]") as hobby_2,
json_extract('{"name":"zhangsan","tel_no":"136-6666-6666","hobbies":["basketball","run","sing"]}',"$.hobbies[2]") as hobby_3,
json_extract('{"name":"zhangsan","tel_no":"136-6666-6666","hobbies":["basketball","run","sing"]}',"$.hobbies[3]") as hobby_4;
name
tel_no
hobby_1
hobby_2
hobby_3
hobby_4
“zhangsan”
136-6666-6666
“basketball”
“run”
“sing”
NULL
3.2 提取json数组的值
site_user表
id
name
tags
1
zhangsan
[“COMMON”]
2
lisi
[“VIP”]
3
wangwu
[“VVIP”,“PLATINUM”]
4
zhaoliu
提取用户的第一个标签:
select
name,
tags,
json_extract(if(LENGTH(tags)>0,tags, '[]'),"$[0]") # 如果tags无数据,赋值为空数组
from site_user;