Deal all, I have a JSON data as follows:
set @injsondata = '{
"action": "SaveEventByMinutes",
"eventbyminutes": [{"matchid":10001,"minute":"1","halftime":"1","value":"A","userid":1,"createddate":"2021-09-25"},
{"matchid":10001,"minute":"2","halftime":"1","value":"X","userid":1,"createddate":"2021-09-25"}]}';
Now I want to change the matchid value to 10002 using select statement in MySQL.
What I have tried
1. select JSON_merge_patch(@injsondata,'{"eventbyminutes[*]":{"matchid": 10002}}');
2. SELECT JSON_replace(@injsondata, '{"eventbyminutes":{"matchid":2}}');
But nothing is working
Is there any other option to do this?
or do I have to create a function or procedure like
DELIMITER $$
CREATE DEFINER=`root`@`localhost` FUNCTION `func_modify_json`(injsondata json) RETURNS json
DETERMINISTIC
begin
declare len int default json_length(injsondata);
declare i int default 0;
declare outjsondata json;
while i < len do
# Replace the report4 property of the i'th element with an empty list
set outjsondata = JSON_replace(injsondata,'$[0].eventbyminutes[i].matchid',10002);
set i = i + 1;
end while;
return outjsondata;
end$$
DELIMITER ;
Something like above?