简体   繁体   中英

json_table syntax and structure

For the first time, I am working with a table in MySQL where some of the fields contain JSON.

I am trying to use the json_table function so that i can use the json data in a relational script. All of the guides on json_table give this type of example syntax:

SELECT people.* 
FROM t1, 
     JSON_TABLE(json_col, '$.people[*]' 
COLUMNS (
                name VARCHAR(40)  PATH '$.name',
                address VARCHAR(100) PATH '$.address')
     ) people;

My issue is that the json data stored in my tables looks like this:

[{"name":"cdennett","address":"123 street","Postcode":"ABCDE"}]

but from all examples of json data i can find it should look like this:

["people": {"name":"cdennett","address":"123 street","Postcode":"ABCDE"}]

I've tried every way possible I can but I get an error message every time, presumably because I am not specifying the array ('$.people[*]') after the json_col in my syntax. But I don't think I have this. Can anyone help?

Your pathing was wrong on the JSON_TABLE function. You want '$[*]' not $.people[*]'

SELECT people.* 
 FROM t1, 
 JSON_TABLE(json_col, '$[*]'
 COLUMNS (
            name VARCHAR(40)  PATH '$.name',
            address VARCHAR(100) PATH '$.address')
 ) people;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM