mysql中json_extract的使用方法实例详解
作者:-王尚可- 发布时间:2024-01-19 04:28:04
一、前言
mysql5.7版本开始支持JSON类型字段,本文详细介绍json_extract函数如何获取mysql中的JSON类型数据
json_extract可以完全简写为 ->
json_unquote(json_extract())可以完全简写为 ->>
下面介绍中大部分会利用简写
二、创建示例表
CREATE TABLE `test_json` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`content` json DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;
# 插入两条测试用的记录
INSERT INTO `test_json` (`content`) VALUES ('{\"name\":\"tom\",\"age\":18,\"score\":[100,90,87],\"address\":{\"province\":\"湖南\",\"city\":\"长沙\"}}');
INSERT INTO `test_json` (`content`) VALUES ('[1, "apple", "red", {"age": 18, "name": "tom"}]');
id | content |
---|---|
1 | {“age”: 18, “name”: “tom”, “score”: [100, 90, 87], “address”: {“city”: “长沙”, “province”: “湖南”}} |
2 | [1, “apple”, “red”, {“age”: 18, “name”: “tom”}] |
三、基本语法
- 获取JSON对象中某个key对应的value值
json_extract函数中,第一个参数content表示json数据,第二个参数为json路径,其中$表示该json数据本身,$.name就表示获取json中key为name的value值
可以利用 -> 表达式来代替json_extract
若获取的val本身为字符串,那么获取的val会被引号包起来,比如"tom",这种数据被解析到程序对象中时,可能会被转义为\“tom\”。为了解决这个问题了,可以在外面再包上一层json_unquote函数,或者使用 ->> 代替->
content:
{“age”: 18, “name”: “tom”, “score”: [100, 90, 87], “address”: {“city”: “长沙”, “province”: “湖南”}}
# 得到"tom"
select json_extract(content,'$.name') from test_json where id = 1;
# 简写方式:字段名->表达式等价于json_extract(字段名,表达式)
select content->'$.name' from test_json where id = 1;
# 结果:
+--------------------------------+
| json_extract(content,'$.name') |
+--------------------------------+
| "tom" |
+--------------------------------+
+-------------------+
| content->'$.name' |
+-------------------+
| "tom" |
+-------------------+
# 解除双引号,得到tom
select json_unquote(json_extract(content,'$.name')) from test_json where id = 1;
# 简写方式:字段名->>表达式等价于json_unquote(json_extract(字段名,表达式))
select content->>'$.name' from test_json where id = 1;
# 结果:
+----------------------------------------------+
| json_unquote(json_extract(content,'$.name')) |
+----------------------------------------------+
| tom |
+----------------------------------------------+
+--------------------+
| content->>'$.name' |
+--------------------+
| tom |
+--------------------+
- 获取JSON数组中某个元素
json_extract函数中,第一个参数content表示json数据,第二个参数为json路径,其中$表示该json数据本身,$[i]表示获取该json数组索引为i的元素(索引从0开始)
与获取key-val一样,若获取的元素为字符串,默认的方式也会得到双引号包起来的字符,导致程序转义,方法也是利用json_unquote函数,或者使用 ->> 代替->
content:
[1, “apple”, “red”, {“age”: 18, “name”: “tom”}]
# 得到"apple"
select json_extract(content,'$[1]') from test_json where id = 2;
# 简写,效果同上
select content->'$[1]' from test_json where id = 2;
# 结果:
+------------------------------+
| json_extract(content,'$[1]') |
+------------------------------+
| "apple" |
+------------------------------+
+-----------------+
| content->'$[1]' |
+-----------------+
| "apple" |
+-----------------+
# 解除双引号,得到apple
select json_unquote(json_extract(content,'$[1]')) from test_json where id = 2;
# 简写,效果同上
select content->>'$[1]' from test_json where id = 2;
# 结果:
+--------------------------------------------+
| json_unquote(json_extract(content,'$[1]')) |
+--------------------------------------------+
| apple |
+--------------------------------------------+
+------------------+
| content->>'$[1]' |
+------------------+
| apple |
+------------------+
- 获取JSON中的嵌套数据
结合前面介绍的两种获取方式,可以获取json数据中的嵌套数据
content: id=1
{“age”: 18, “name”: “tom”, “score”: [100, 90, 87], “address”: {“city”: “长沙”, “province”: “湖南”}}
content: id=2
[1, “apple”, “red”, {“age”: 18, “name”: “tom”}]
# 得到:87
select content->'$.score[2]' from test_json where id = 1;
# 结果:
+-----------------------+
| content->'$.score[2]' |
+-----------------------+
| 87 |
+-----------------------+
# 得到:18
select content->'$[3].age' from test_json where id = 2;
# 结果:
+---------------------+
| content->'$[3].age' |
+---------------------+
| 18 |
+---------------------+
四、渐入佳境
- 获取JSON多个路径的数据
将会把多个路径的数据组合成数组返回
content: id=1
{“age”: 18, “name”: “tom”, “score”: [100, 90, 87], “address”: {“city”: “长沙”, “province”: “湖南”}}
select json_extract(content,'$.age','$.score') from test_json where id = 1;
# 结果:
+-----------------------------------------+
| json_extract(content,'$.age','$.score') |
+-----------------------------------------+
| [18, [100, 90, 87]] |
+-----------------------------------------+
select json_extract(content,'$.name','$.address.province','$.address.city') from test_json where id = 1;
# 结果:
+----------------------------------------------------------------------+
| json_extract(content,'$.name','$.address.province','$.address.city') |
+----------------------------------------------------------------------+
| ["tom", "湖南", "长沙"] |
+----------------------------------------------------------------------+
- 路径表达式*的使用
将会把多个路径的数据组合成数组返回
# 先插入一条用于测试的数据
INSERT INTO `test_json` (`id`,`content`) VALUES(3,'{"name":"tom","address":{"name":"中央公园","city":"长沙"},"class":{"id":3,"name":"一年三班"},"friend":[{"age":20,"name":"marry"},{"age":21,"name":"Bob"}]}')
content: id=3
{“name”: “tom”, “class”: {“id”: 3, “name”: “一年三班”}, “friend”: [{“age”: 20, “name”: “marry”}, {“age”: 21, “name”: “Bob”}], “address”: {“city”: “长沙”, “name”: “中央公园”}}
# 获取所有二级嵌套中key=name的值
# 由于friend的二级嵌套是一个数组,所以.name获取不到其中的所有name值
select content->'$.*.name' from test_json where id = 3;
+----------------------------------+
| content->'$.*.name' |
+----------------------------------+
| ["一年三班", "中央公园"] |
+----------------------------------+```
# 获取所有key为name值的数据,包括任何嵌套内的name
select content->'$**.name' from test_json where id = 3;
+---------------------------------------------------------+
| content->'$**.name' |
+---------------------------------------------------------+
| ["tom", "一年三班", "marry", "Bob", "中央公园"] |
+---------------------------------------------------------+
# 获取数组中所有的name值
select content->'$.friend[*].name' from test_json where id = 3;
+-----------------------------+
| content->'$.friend[*].name' |
+-----------------------------+
| ["marry", "Bob"] |
+-----------------------------+
- 返回NULL值
content: id=1
{“age”: 18, “name”: “tom”, “score”: [100, 90, 87], “address”: {“city”: “长沙”, “province”: “湖南”}}
寻找的JSON路径都不存在
# age路径不存在,返回NULL
# 若有多个路径,只要有一个路径存在则不会返回NULL
select json_extract(content,'$.price') from test_json where id = 1;
+---------------------------------+
| json_extract(content,'$.price') |
+---------------------------------+
| NULL |
+---------------------------------+
路径中有NULL
# 存在任意路径为NULL则返回NULL
select json_extract(content,'$.age',NULL) from test_json where id = 1;
+------------------------------------+
| json_extract(content,'$.age',NULL) |
+------------------------------------+
| NULL |
+------------------------------------+
- 返回错误
若第一个参数不是JSON类型的数据,则返回错误
select json_extract('{1,2]',$[0])
若路径表达式不规范,则返回错误
select content->'$age' from test_json where id = 1;
# 结果:
ERROR 3143 (42000): Invalid JSON path expression. The error is around character position 1.
五、使用场景
JSON_EXTRACT函数通常用于要获取JSON中某个特定的数据或者要根据它作为判断条件时使用
六、参考文档
mysql5.7官方文档
https://www.sjkjc.com/mysql-ref/json_extract
来源:https://blog.csdn.net/AJakey/article/details/128113607


猜你喜欢
- 想要在准备查询混合数据库系统时将SQL Server转换为 XQuery 和进行反向转换吗?了解如何开发 Web 服务来从SQL Serve
- 情感短文本分类TextRNN是一种循环神经网络(RNN)结构,特别适用于处理序列数据。它通过将上一个时刻的隐状态与当前时刻的输入进行结合,来
- Django添加静态文件有两种方法:首先setting.py配置文件中添加静态文件的路径:STATICFILES_DIRS = [ os.p
- 本文实例为大家分享了Python管理Windows服务的具体代码,供大家参考,具体内容如下#!/usr/bin/python# encodi
- 大家都知道一些论坛的标题有高亮显示功能,在这里我不讨论也不研究论坛普遍的实现方法,下面是我的实现方法:实现思路:把要高亮显示的标题加上特定标
- 在处理数据和进行机器学习的时候,遇到了大量需要处理的时间序列。比如说:数据库读取的str和time的转化,还有time的差值计算。总结一下p
- CREATE TABLE table1( [ID] [bigint] IDENTITY(1,1) NOT NULL, [Name] [nva
- 1.加载数据库,数据库的配置不能写死在seting.py文件中,下面的方式是读取另外一个文件,配置数据库:config = '
- 行和列的位置都在以下三个列表中的一列中,则对应位置为1,其余位置全为0——[7-56,239-327,438-454,522-556,574
- 方式1:在pygame中使用pygame.event.get()方法捕获键盘事件,使用这个方式捕获的键盘事件必须要是按下再弹起才算一次。示例
- 一、基本概念查找(Searching)就是根据给定的某个值,在查找表中确定一个其关键字等于给定值的数据元素(或记录)。查找表(Search
- 上篇文章给大家介绍了Python爬虫实现百度翻译功能过程详解Python爬虫学习之翻译小程序 感兴趣的朋友点击查看。今天给大家介
- 你是不是觉得每次新建项目都要写一次# coding:utf-8,感觉特烦人呐!懒(fu)人(li)教程来啦,先看效果图吧中文版如图进入设置然
- 本文实例讲述了微信公众平台实现获取用户OpenID的方法。分享给大家供大家参考。具体分析如下:用户点击微信自定义菜单view类型按钮后,微信
- 虽然小型web应用程序用单个脚本可以很方便,但这种方法却不能很好地扩展。随着应用变得复杂,在单个大的源文件中处理会变得问题重重。与大多数其他
- 本文以实例形式展示了Yii使用find findAll查找出指定字段的实现方法,分享给大家供大家参考之用。具体方法如下:总所周知,采用如下方
- 原由定期更换密码是一种非常重要的安全措施,这种做法可以有效地保护你的账户和个人信息不受黑客和网络攻击者的侵害。密码泄露是一个非常普遍的问题,
- isalnum()方法检查判断字符串是否包含字母数字字符。语法以下是isalnum()方法的语法:str.isa1num()参数
- 通过show variables like 'character_set%%';查看编码修改mysql的编码方式可以有以下几
- 前言:作为测试工程师都知道,json格式的文件使我们常用的一种数据存放形式,那么对于python文件的处理,python语言有着得天独厚的条