In this moment I'm using SQLSERVER to store json data into a Table. I do it using OPENJSON. Here is an example how I do it:
INSERT INTO myTable (ColA, ColB, ColC)
SELECT jDataA, jDataB, jDataC
FROM OPENJSON (@JSON, '$.data')
WITH (jDataA varchar(4),
jDataB int,
jDataC varchar(48));
The json data is:
{
"data": [{
"jDataA": "AAAA",
"jDataB": 1,
"jDataC": "OK"
}, {
"jDataA": "BBBB",
"jDataB": 2,
"jDataC": "OK"
}, {
"jDataA": "CCCC",
"jDataB": 3,
"jDataC": "BAD"
}, {
"jDataA": "DDDD",
"jDataB": 4,
"jDataC": "BAD"
}
]
}
Now I want to migrate to MySQL (8.0), but I can't find something like OPENJSON. There are functions like JSON_EXTRACT, but I feel it is pretty limited.
Can you tell me how to implement an "insert from json" using mysql?
This can be done using JSON_TABLE
function as follows :
SELECT jDataA, jDataB, jDataC
FROM JSON_TABLE (
@JSON,
'$.data[*]'
COLUMNS(
jDataA varchar(4) PATH '$.jDataA',
jDataB varchar(4) PATH '$.jDataB',
jDataC varchar(4) PATH '$.jDataC'
)
) as s