当前位置:网站首页> 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'
以上为个人经验,希望能给大家一个参考,也希望大家多多支持软件开发网。
边栏推荐
猜你喜欢
Leetcode brush question: binary tree 13 (the same tree)
Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
零道云新UI设计中
Leetcode skimming: binary tree 10 (number of nodes of a complete binary tree)
【数字IC验证快速入门】2、通过一个SoC项目实例,了解SoC的架构,初探数字系统设计流程
Zero cloud new UI design
【数字IC验证快速入门】8、数字IC中的典型电路及其对应的Verilog描述方法
【数字IC验证快速入门】1、浅谈数字IC验证,了解专栏内容,明确学习目标
欢迎来战,赢取丰厚奖金:Code Golf 代码高尔夫挑战赛正式启动
小程序页面导航
随机推荐
Introduction to dead letter queue (two consumers, one producer)
Leetcode brush questions: binary tree 18 (largest binary tree)
When JS method passes long type ID value, precision loss will occur
14、Transformer--VIT TNT BETR
点云文件的.dat文件读取保存
常用的视图容器类组件
Informatics Olympiad 1337: [example 3-2] word search tree | Luogu p5755 [noi2000] word search tree
DP: tree DP
Scala基础【HelloWorld代码解析,变量和标识符】
Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
Schema and model
银河证券在网上开户安全吗?
信息学奥赛一本通 1337:【例3-2】单词查找树 | 洛谷 P5755 [NOI2000] 单词查找树
How to choose a good external disk platform, safe and formal?
Enter the parallel world
National Eye Care Education Conference, 2022 the Fourth Beijing International Youth eye health industry exhibition
走入并行的世界
Leetcode(695)——岛屿的最大面积
sort和投影
【数字IC验证快速入门】3、数字IC设计全流程介绍