博客
关于我
mysql中json_extract的使用方法
阅读量:790 次
发布时间:2023-02-11

本文共 2958 字,大约阅读时间需要 9 分钟。

MySQL 中 JSON_EXTRACT 的使用方法

前言

MySQL 5.7 版本后开始支持 JSON 类型字段,本文将详细介绍 JSON_EXTRACT 函数的使用方法,帮助您获取 MySQL 中的 JSON 数据。

JSON_EXTRACT 函数可以通过简写形式进行调用,具体如下:

  • json_extract 可以完全简写为 ->
  • json_unquote(json_extract()) 可以完全简写为 ->>

本文将基于这些简写形式进行介绍。


创建示例表

为了演示 JSON_EXTRACT 的使用,我们先创建一个示例表 test_json,字段 content 为 JSON 类型。

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": {"province": "湖南", "city": "长沙"}}
2 [1, "apple", "red", {"age": 18, "name": "tom"}]

基本语法

获取 JSON 中某个 key 对应的 value 值

JSON_EXTRACT 函数的第一个参数是 JSON 数据,第二个参数是 JSON 路径。

  • 路径 $.name 表示获取 JSON 数据中 key 为 "name" 的 value。
  • 路径 $[i] 表示获取 JSON 数组中索引为 i 的元素。

示例:

-- 获取 "tom"select json_extract(content, '$.name') from test_json where id = 1;-- 简写方式select content -> '$.name' from test_json where id = 1;

如果 value 本身是字符串,会被引号包裹,导致转义。为了去除引号,可以使用 json_unquote

-- 解除引号,得到 "tom"select json_unquote(json_extract(content, '$.name')) from test_json where id = 1;-- 简写方式select content ->> '$.name' from test_json where id = 1;

获取 JSON 中某个元素

如果需要获取 JSON 数组中的某个元素,可以使用索引。

-- 获取 "apple"select json_extract(content, '$[1]') from test_json where id = 2;-- 简写方式select content -> '$[1]' from test_json where id = 2;

同样,数组中的元素如果是字符串,会被引号包裹,使用 json_unquote 可以去除引号。

-- 解除引号,得到 "apple"select json_unquote(json_extract(content, '$[1]')) from test_json where id = 2;-- 简写方式select content ->> '$[1]' from test_json where id = 2;

获取嵌套数据

JSON_EXTRACT 可以处理嵌套的 JSON 数据。

  • 获取嵌套对象中的 "score" 值。
  • 获取嵌套对象中的 "age" 值。

示例:

-- 获取 87select content -> '$.score[2]' from test_json where id = 1;-- 获取 18select content -> '$[3].age' from test_json where id = 2;

渐入佳境

获取多个路径的数据

JSON_EXTRACT 可以返回多个路径的数据组合成数组。

示例:

-- 获取 age 和 scoreselect json_extract(content, '$.age', '$.score') from test_json where id = 1;-- 结果:[18, [100, 90, 87]]-- 获取 name、地址省市select json_extract(content, '$.name', '$.address.province', '$.address.city') from test_json where id = 1;-- 结果:["tom", "湖南", "长沙"]

路径表达式 * 的使用

* 可以表示匹配所有符合条件的键。

示例:

-- 获取所有 key 为 "name" 的值select content -> '$*.name' from test_json where id = 3;-- 结果:["一年三班", "中央公园"]-- 获取所有嵌套中的 name 值select content -> '$**.name' from test_json where id = 3;-- 结果:["tom", "一年三班", "marry", "Bob", "中央公园"]-- 获取朋友数组中的 nameselect content -> '$.friend[*].name' from test_json where id = 3;-- 结果:["marry", "Bob"]

返回 NULL 值

如果 JSON 路径不存在,JSON_EXTRACT 会返回 NULL。

示例:

-- 寻找不存在的路径select json_extract(content, '$.price') from test_json where id = 1;-- 结果:NULL

使用场景

JSON_EXTRACT 函数适用于以下场景:

  • 提取 JSON 数据中的特定字段。
  • 处理嵌套 JSON 数据。
  • 执行复杂的 JSON 路径操作。

通过合理运用 JSON_EXTRACT,您可以高效地提取和处理 JSON 数据,满足开发需求。

转载地址:http://wpbfk.baihongyu.com/

你可能感兴趣的文章
Manjaro 24.1 “Xahea” 发布!具有 KDE Plasma 6.1.5、GNOME 46 和最新的内核增强功能
查看>>
mapping文件目录生成修改
查看>>
MapReduce程序依赖的jar包
查看>>
mariadb multi-source replication(mariadb多主复制)
查看>>
MariaDB的简单使用
查看>>
MaterialForm对tab页进行隐藏
查看>>
Member var and Static var.
查看>>
memcached高速缓存学习笔记001---memcached介绍和安装以及基本使用
查看>>
memcached高速缓存学习笔记003---利用JAVA程序操作memcached crud操作
查看>>
Memcached:Node.js 高性能缓存解决方案
查看>>
memcache、redis原理对比
查看>>
memset初始化高维数组为-1/0
查看>>
Metasploit CGI网关接口渗透测试实战
查看>>
Metasploit Web服务器渗透测试实战
查看>>
MFC模态对话框和非模态对话框
查看>>
Moment.js常见用法总结
查看>>
MongoDB出现Error parsing command line: unrecognised option ‘--fork‘ 的解决方法
查看>>
mxGraph改变图形大小重置overlay位置
查看>>
MongoDB可视化客户端管理工具之NoSQLbooster4mongo
查看>>
Mongodb学习总结(1)——常用NoSql数据库比较
查看>>