Home  >  Article  >  Database  >  How to parse json array in mysql

How to parse json array in mysql

PHPz
PHPzforward
2023-05-29 09:37:053112browse

mysql parsing json array

Mysql started to support json parsing in 5.7. You can also parse arrays!

Go directly to the 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 can parse sql, tpl is the key value of your json

If it is an array, use $[*].url or $[0] .url Get all the values ​​or the url of a certain subscript

The demo below can be copied directly to sql and run

 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":"zhangyusheng@xxx.com","mobile":"123123","trueName":"张昱升","isEmployee":true}}}', "$.tpl"), "$.items"), "$[0].url");

Let’s analyze it

The original json is

{
    "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":"zhangyusheng@xxx.com",
            "mobile":"23123",
            "trueName":"张昱升",
            "isEmployee":true
        }
    }
}
  • $.tpl is to get the tpl key key

  • $[0].url is Get the url value of the first object of [{url:1},{url:2}], which is 1

##mysql json string is parsed into the corresponding field

Field name: mobile, content: {"contactName":"Segment XX","contactJobTitle":"To be confirmed","contactMobile":"131XXXXXXX"}.

Solution: JSON_EXTRACT

Execute SQL:

How to parse json array in mysql## Query results:

How to parse json array in mysqlThe result is in quotes and cannot really be used.

Solution: REPLACE

Execute SQL:

How to parse json array in mysqlQuery results:

##problem solved. How to parse json array in mysql

sql statement:

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'

The above is the detailed content of How to parse json array in mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete