I have a JSON like below:
[
{
"Label":"rm_sensor_combined",
"Data":"[{\"Label\":\"name\",\"Value\":\"esd001\",\"Type\":\"String\"},{\"Label\":\"dname\",\"Value\":\"esd\",\"Type\":\"String\"},{\"Label\":\"location\",\"Value\":\"here\",\"Type\":\"String\"},{\"Label\":\"kill\",\"Value\":\"yes\",\"Type\":\"String\"}]",
"Where":"[{\"Label\":\"ExternLineNo\",\"Value\":\"10\",\"Type\":\"String\"},{\"Label\":\"ExternReceiptKey\",\"Value\":\"20\",\"Type\":\"String\"},{\"Label\":\"SKU\",\"Value\":\"got it\",\"Type\":\"String\"}]",
"Option":"INSERT"
}
]
I would like to store them into a table using a store procedure, the table should look like the below:
Label | Data | Where | Option |
---|---|---|---|
rm_sensor_combined | data Json | Where Json | insert |
any advice would be appreciated. Thanks
below is my stored procedure like:
CREATE PROCEDURE `sp_getSaveJsonData`(ajs_param json)
BEGIN
drop table if exists ToSaveTable;
CREATE TEMPORARY TABLE ToSaveTable (
`Label` LONGTEXT,
`Data` LONGTEXT,
`Where` LONGTEXT,
`Option` nvarchar(1000)
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
SELECT JSON_EXTRACT(ajs_param, '$[*]') INTO @LblArray;
SELECT JSON_LENGTH(@LblArray) INTO @jsonLength;
set @counter = 0;
while @counter < @jsonLength do
select json_extract(@LblArray, concat('$[',@counter,']')) into @rowData;
insert into fromJsonTable(`Label`, `Data`, `Where`, `Option`)
values( REPLACE(json_extract(@rowData, '$.Label'), '"', ''),
REPLACE(json_extract(@rowData, '$.Data'), '"', ''),
REPLACE(json_extract(@rowData, '$.Where'), '"', ''),
REPLACE(json_extract(@rowData, '$.Option'), '"', '')
);
set @counter = @counter + 1;
end while;
END