I have a JSON array, I want to give an entry and this entry should be removed from the JSON array.
For example, the array:
SET @os ='[
{
"id": "66",
"date_end": "2022-01-09 18:00:00",
"occupied": "1",
"date_begin": "2022-01-09 17:30:00",
"idCategory": "[1,2,3]",
"date_requested": "2022-01-02 14:59:27"
},
{
"id": "39",
"date_end": "2022-01-03 11:30:00",
"occupied": "1",
"date_begin": "2022-01-03 08:00:00",
"idCategory": "3",
"date_requested": "2022-01-01 22:40:58"
}
]';
And the entry that I want to be removed:
SET @test ='
{
"id": "39",
"date_end": "2022-01-03 11:30:00",
"occupied": "1",
"date_begin": "2022-01-03 08:00:00",
"idCategory": "3",
"date_requested": "2022-01-01 22:40:58"
}';
My code is something like:
SET @new_os = (select json_arrayagg(js) from json_table(@os, '$[*]' columns ( js json path '$')) as jt
where json_extract(js, '$.date_end') <> json_extract(@test, '$.date_end') and json_extract(js, '$.id') <> json_extract(@test, '$.id')
and json_extract(js, '$.date_begin') <> json_extract(@test, '$.date_begin') and json_extract(js, '$.date_requested') <> json_extract(@test, '$.date_requested')
and json_extract(js, '$.idCategory') <> json_extract(@test, '$.idCategory') and json_extract(js, '$.occupied') <> json_extract(@test, '$.occupied'));
SELECT @os,@new_os;
But in the end I get that @new_os is NULL, and it should be:
[
{
"id": "66",
"date_end": "2022-01-09 18:00:00",
"occupied": "1",
"date_begin": "2022-01-09 17:30:00",
"idCategory": "[1,2,3]",
"date_requested": "2022-01-02 14:59:27"
}]