当前位置:网站首页> 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'
以上为个人经验,希望能给大家一个参考,也希望大家多多支持软件开发网。
边栏推荐
- 基础篇——配置文件解析
- Dry goods navigation in this quarter | Q2 2022
- Usaco3.4 "broken Gong rock" band raucous rockers - DP
- Introduction to dead letter queue (two consumers, one producer)
- CTF逆向基础
- 常用的视图容器类组件
- 计算lnx的一种方式
- Is it safe to open an account online? Where can I get a low commission?
- Informatics Olympiad 1340: [example 3-5] extended binary tree
- 小程序全局配置
猜你喜欢
基础篇——配置文件解析
Fundamentals - configuration file analysis
Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
[quick start of Digital IC Verification] 9. Finite state machine (FSM) necessary for Verilog RTL design
Applet page navigation
2022北京眼睛健康用品展,护眼产品展,中国眼博会11月举办
Oracle-表空间管理
如何形成规范的接口文档
About the priority of Bram IP reset
Leetcode skimming: binary tree 17 (construct binary tree from middle order and post order traversal sequence)
随机推荐
kubernetes资源对象介绍及常用命令(五)-(ConfigMap&Secret)
Leetcode (695) - the largest area of an island
2.8 basic knowledge of project management process
基金网上开户安全吗?去哪里开,可以拿到低佣金?
IC科普文:ECO的那些事儿
Rainbond 5.7.1 支持对接多家公有云和集群异常报警
Schema和Model
小程序页面导航
Sort and projection
【刷题记录】1. 两数之和
信息学奥赛一本通 1340:【例3-5】扩展二叉树
无卷积骨干网络:金字塔Transformer,提升目标检测/分割等任务精度(附源代码)...
Dry goods navigation in this quarter | Q2 2022
model方法
Nprogress plug-in progress bar
Zero cloud new UI design
E. Singhal and numbers (prime factor decomposition)
ICTCLAS word Lucene 4.9 binding
Hongmeng OS' fourth learning
go 文件路径操作