当前位置:网站首页> 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'以上为个人经验,希望能给大家一个参考,也希望大家多多支持软件开发网。
边栏推荐
- Welcome to the game and win rich bonuses: Code Golf Challenge officially launched
- Leetcode skimming: binary tree 17 (construct binary tree from middle order and post order traversal sequence)
- Codeforces Round #804 (Div. 2) - A, B, C
- 资源道具化
- Guidelines for application of Shenzhen green and low carbon industry support plan in 2023
- 鸿蒙os第四次学习
- CVPR 2022 | common 3D damage and data enhancement
- A solution to PHP's inability to convert strings into JSON
- Wechat applet regular expression extraction link
- Propping of resources
猜你喜欢

【数字IC验证快速入门】2、通过一个SoC项目实例,了解SoC的架构,初探数字系统设计流程

【愚公系列】2022年7月 Go教学课程 004-Go代码注释

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

Introduction to dead letter queue (two consumers, one producer)

CTF reverse Foundation

Convolution free backbone network: Pyramid transformer to improve the accuracy of target detection / segmentation and other tasks (with source code)

港股将迎“最牛十元店“,名创优品能借IPO突围?

Unity编辑器扩展 UI控件篇

Leetcode (695) - the largest area of an island

无卷积骨干网络:金字塔Transformer,提升目标检测/分割等任务精度(附源代码)...
随机推荐
Fundamentals - configuration file analysis
About the priority of Bram IP reset
【愚公系列】2022年7月 Go教学课程 004-Go代码注释
CCPC 2021 Weihai - G. shinyruo and KFC (combination number, tips)
Leetcode skimming: binary tree 16 (path sum)
model方法
鸿蒙os第四次学习
Leetcode(695)——岛屿的最大面积
js方法传Long类型id值时会出现精确损失
ROS2专题【01】:win10上安装ROS2
2020 CCPC Weihai - A. golden spirit (thinking), D. ABC project (big number decomposition / thinking)
本季度干货导航 | 2022年Q2
Scala basics [HelloWorld code parsing, variables and identifiers]
Applet project structure
[quick start of Digital IC Verification] 7. Basic knowledge of digital circuits necessary for verification positions (including common interview questions)
Leetcode (347) - top k high frequency elements
Zero cloud new UI design
CVPR 2022 | 常见3D损坏和数据增强
[Yugong series] go teaching course in July 2022 004 go code Notes
IC科普文:ECO的那些事儿