当前位置:网站首页> 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'以上为个人经验,希望能给大家一个参考,也希望大家多多支持软件开发网。
边栏推荐
- Oracle tablespace management
- [quick start of Digital IC Verification] 9. Finite state machine (FSM) necessary for Verilog RTL design
- 【数字IC验证快速入门】9、Verilog RTL设计必会的有限状态机(FSM)
- Y57. Chapter III kubernetes from entry to proficiency -- business image version upgrade and rollback (30)
- National Eye Care Education Conference, 2022 the Fourth Beijing International Youth eye health industry exhibition
- 证券开户选择哪个证券比较好?网上开户安全么?
- 小程序项目结构
- 小程序全局配置
- Leetcode brush question: binary tree 14 (sum of left leaves)
- [Yugong series] go teaching course in July 2022 004 go code Notes
猜你喜欢

Oracle-表空间管理

Hongmeng OS' fourth learning

小程序事件绑定

Wechat applet regular expression extraction link

kubernetes资源对象介绍及常用命令(五)-(ConfigMap&Secret)

鸿蒙os第四次学习

B站UP搭建世界首个纯红石神经网络、基于深度学习动作识别的色情检测、陈天奇《机器学编译MLC》课程进展、AI前沿论文 | ShowMeAI资讯日报 #07.05
![[quick start of Digital IC Verification] 3. Introduction to the whole process of Digital IC Design](/img/92/7af0db21b3d7892bdc5dce50ca332e.png)
[quick start of Digital IC Verification] 3. Introduction to the whole process of Digital IC Design

2.8 basic knowledge of project management process

Classic implementation of the basic method of intelligent home of Internet of things
随机推荐
E. Singhal and Numbers(质因数分解)
[record of question brushing] 1 Sum of two numbers
Informatics Olympiad 1340: [example 3-5] extended binary tree
14、Transformer--VIT TNT BETR
3.3、项目评估
小程序全局配置
Nprogress plug-in progress bar
Simple understanding of interpolation search
JS implementation prohibits web page zooming (ctrl+ mouse, +, - zooming effective pro test)
实操演示:产研团队如何高效构建需求工作流?
小程序页面导航
Sort and projection
[C language] three implementations of quick sorting and optimization details
Usaco3.4 "broken Gong rock" band raucous rockers - DP
Point cloud file Dat file read save
[quick start of Digital IC Verification] 6. Quick start of questasim (taking the design and verification of full adder as an example)
炒股开户最低佣金,低佣金开户去哪里手机上开户安全吗
【数字IC验证快速入门】3、数字IC设计全流程介绍
【数字IC验证快速入门】6、Questasim 快速上手使用(以全加器设计与验证为例)
Hongmeng OS' fourth learning