当前位置:网站首页> mysql全面解析json/数组
mysql全面解析json/数组
2022-07-05 20:35:00 【1024问】
mysql解析json数组
直接上demo
下面这个demo可以直接复制到sql运行
我们来分析一下
mysql json字符串解析成对应字段
解决方法:JSON_EXTRACT
解决方法:REPLACE
mysql解析json数组mysql在5.7开始支持json解析了 也可以解析数组哦!
直接上demo SELECT Substr(col, 2, Length(col) - 2), Length(col) FROM (SELECT Json_extract(Json_extract(Json_extract(state, "$.tpl"),"$.items" ), "$[0].url") AS col FROM page ORDER BY id DESC LIMIT 100) t;JSON_EXTRACT可以解析sql , tpl就是你json的key值
如果是数组,用$[*].url 或者 $[0].url 获取全部的value 或者某个下标的url
下面这个demo可以直接复制到sql运行 select JSON_EXTRACT(JSON_EXTRACT(JSON_EXTRACT('{"tpl":{"items":[{"type":"image","config":{"expandable":true,"linkAble":true},"url":"https://fs.esf.fangdd.net/test/FiZ0OtkhTZoD7fOtkp55SnuLGiKu.png?imageView2/2/w/750","id":1542348252537},{"type":"image","config":{"expandable":true,"linkAble":true},"url":"https://fs.esf.fangdd.net/test/FlR1VDQWEzD406NosLFrJUez4g_X.png?imageView2/2/w/750","id":1542348263477},{"type":"image","config":{"expandable":true,"linkAble":true},"url":"https://fs.esf.fangdd.net/test/FhMuYkWvnoMbv8I1dlQbm1KaX5Kn.png?imageView2/2/w/750","id":1542348269599},{"type":"image","config":{"expandable":true,"linkAble":true},"url":"https://fs.esf.fangdd.net/test/FlgR4IUNElPbcgjN2re_9A8jX30v.png?imageView2/2/w/750","id":1542348276124},{"type":"image","config":{"expandable":true,"linkAble":true},"url":"https://fs.esf.fangdd.net/test/FpXF8ETHxU8aqriiKbsYDjnu2Xd5.png?imageView2/2/w/750","id":1542348282561},{"type":"image","config":{"expandable":true,"linkAble":true},"url":"https://fs.esf.fangdd.net/test/FkUz5m7Jd6kE2slSyreDucozc3XH.png?imageView2/2/w/750","id":1542348288150,"link":"http://www.baidu.com"}],"bottomItems":[],"title":"demo2","description":"","wxLogo":"","bodyStyleInline":{},"bg":"","bgType":"","bottomStyleInline":{},"bottomBg":"","bottomBgType":"","uuid":"aaef8dfe-256a-4559-aec9-95d1fcdcf830","activeItemsName":"items","activeImgType":"","authInfo":{"role_list":[{"name":"test","access_key_list":[]},{"name":"审核人员","access_key_list":[]}],"city_list":[],"userId":3108779,"userName":"zhangyusheng","email":"[email protected]","mobile":"123123","trueName":"张昱升","isEmployee":true}}}', "$.tpl"), "$.items"), "$[0].url");我们来分析一下原始json为
{ "tpl":{ "items":[ { "type":"image", "config":{ "expandable":true, "linkAble":true }, "url":"https://fs.esf.fangdd.net/test/FiZ0OtkhTZoD7fOtkp55SnuLGiKu.png?imageView2/2/w/750", "id":1542348252537 }, { "type":"image", "config":{ "expandable":true, "linkAble":true }, "url":"https://fs.esf.fangdd.net/test/FlR1VDQWEzD406NosLFrJUez4g_X.png?imageView2/2/w/750", "id":1542348263477 }, { "type":"image", "config":{ "expandable":true, "linkAble":true }, "url":"https://fs.esf.fangdd.net/test/FhMuYkWvnoMbv8I1dlQbm1KaX5Kn.png?imageView2/2/w/750", "id":1542348269599 }, { "type":"image", "config":{ "expandable":true, "linkAble":true }, "url":"https://fs.esf.fangdd.net/test/FlgR4IUNElPbcgjN2re_9A8jX30v.png?imageView2/2/w/750", "id":1542348276124 }, { "type":"image", "config":{ "expandable":true, "linkAble":true }, "url":"https://fs.esf.fangdd.net/test/FpXF8ETHxU8aqriiKbsYDjnu2Xd5.png?imageView2/2/w/750", "id":1542348282561 }, { "type":"image", "config":{ "expandable":true, "linkAble":true }, "url":"https://fs.esf.fangdd.net/test/FkUz5m7Jd6kE2slSyreDucozc3XH.png?imageView2/2/w/750", "id":1542348288150, "link":"http://www.baidu.com" } ], "bottomItems":[ ], "title":"demo2", "description":"", "wxLogo":"", "bodyStyleInline":{ }, "bg":"", "bgType":"", "bottomStyleInline":{ }, "bottomBg":"", "bottomBgType":"", "uuid":"aaef8dfe-256a-4559-aec9-95d1fcdcf830", "activeItemsName":"items", "activeImgType":"", "authInfo":{ "role_list":[ { "name":"test", "access_key_list":[ ] }, { "name":"审核人员", "access_key_list":[ ] } ], "city_list":[ ], "userId":3108779, "userName":"zhangyusheng", "email":"[email protected]", "mobile":"23123", "trueName":"张昱升", "isEmployee":true } }} $.tpl就是获取tpl这个键key
$[0].url就是获取[{url:1},{url:2}] 这个数组第一个对象的url值 也就是1
字段名 :mobile ,内容:{"contactName":"段XX","contactJobTitle":"待确认","contactMobile":"131XXXXXXX"}。
解决方法:JSON_EXTRACT执行SQL:

查询结果:

结果带引号,并不能真正使用。
解决方法:REPLACE执行SQL:

查询结果:

问题解决。
sql语句:
SELECTREPLACE (JSON_EXTRACT (mobile, '$.contactName'),'"','') AS 'contactName',REPLACE (JSON_EXTRACT (mobile, '$.contactMobile'),'"','') AS 'contactMobile',REPLACE (JSON_EXTRACT (mobile, '$.contactJobTitle'),'"','') AS 'contactJobTitle'FROMcscw_clientWHEREid = 'XXXXXXXXXXXXXXX'以上为个人经验,希望能给大家一个参考,也希望大家多多支持软件开发网。
边栏推荐
- . Net distributed transaction and landing solution
- July 4, 2022 - July 10, 2022 (UE4 video tutorial MySQL)
- Fundamentals - configuration file analysis
- Informatics Olympiad 1338: [example 3-3] hospital setting | Luogu p1364 hospital setting
- 物联网智能家居基本方法实现之经典
- 小程序事件绑定
- 2020 CCPC 威海 - A. Golden Spirit(思维),D. ABC Conjecture(大数分解 / 思维)
- A way to calculate LNX
- E. Singhal and numbers (prime factor decomposition)
- Codeforces Round #804 (Div. 2) - A, B, C
猜你喜欢

【数字IC验证快速入门】7、验证岗位中必备的数字电路基础知识(含常见面试题)

欢迎来战,赢取丰厚奖金:Code Golf 代码高尔夫挑战赛正式启动

Practical demonstration: how can the production research team efficiently build the requirements workflow?

Applet global configuration

Wechat applet regular expression extraction link

Oracle-表空间管理

Scala基础【HelloWorld代码解析,变量和标识符】
![[quick start of Digital IC Verification] 1. Talk about Digital IC Verification, understand the contents of the column, and clarify the learning objectives](/img/90/88a1f79a07016738d2688548e21949.png)
[quick start of Digital IC Verification] 1. Talk about Digital IC Verification, understand the contents of the column, and clarify the learning objectives

National Eye Care Education Conference, 2022 the Fourth Beijing International Youth eye health industry exhibition

【数字IC验证快速入门】1、浅谈数字IC验证,了解专栏内容,明确学习目标
随机推荐
Leetcode skimming: binary tree 16 (path sum)
CCPC 2021 Weihai - G. shinyruo and KFC (combination number, tips)
信息学奥赛一本通 1338:【例3-3】医院设置 | 洛谷 P1364 医院设置
Reinforcement learning - learning notes 4 | actor critical
2.8 basic knowledge of project management process
Leetcode: binary tree 15 (find the value in the lower left corner of the tree)
Leetcode brush question: binary tree 14 (sum of left leaves)
[quick start of Digital IC Verification] 1. Talk about Digital IC Verification, understand the contents of the column, and clarify the learning objectives
19 mongoose modularization
基金网上开户安全吗?去哪里开,可以拿到低佣金?
小程序代码的构成
欢迎来战,赢取丰厚奖金:Code Golf 代码高尔夫挑战赛正式启动
Oracle-表空间管理
14、Transformer--VIT TNT BETR
银河证券在网上开户安全吗?
Wechat applet regular expression extraction link
Frequent MySQL operations cause table locking problems
Station B up builds the world's first pure red stone neural network, pornographic detection based on deep learning action recognition, Chen Tianqi's course progress of machine science compilation MLC,
B站UP搭建世界首个纯红石神经网络、基于深度学习动作识别的色情检测、陈天奇《机器学编译MLC》课程进展、AI前沿论文 | ShowMeAI资讯日报 #07.05
无卷积骨干网络:金字塔Transformer,提升目标检测/分割等任务精度(附源代码)...