mysql全面解析json/數組

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

mysql json字符串解析成對應字段

字段名 :mobile ,內容:{"contactName":"段XX","contactJobTitle":"待確認","contactMobile":"131XXXXXXX"}。

解決方法:JSON_EXTRACT

執行SQL:

查詢結果:

結果帶引號,並不能真正使用。

解決方法:REPLACE

執行SQL:

查詢結果:

問題解決。

sql語句:

SELECT
REPLACE (
JSON_EXTRACT (mobile, '$.contactName'),
'"',
''
) AS 'contactName',
REPLACE (
JSON_EXTRACT (mobile, '$.contactMobile'),
'"',
''
) AS 'contactMobile',
REPLACE (
JSON_EXTRACT (mobile, '$.contactJobTitle'),
'"',
''
) AS 'contactJobTitle'
FROM
cscw_client
WHERE
id = 'XXXXXXXXXXXXXXX'

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: