I have a table with json-column in the column data
and a table with keys to delete from the first table. MySQL 5.7.
id | data |
---|---|
1 | {"key1": "value1", "key2": "value2", "key10": "value10", "key100": "value100"} |
20 | {"key1": "value1", "key18": "value18", "key150": "value150"} |
57 | {"key5": "value5", "key10": "value10"} |
id | key | value |
---|---|---|
1 | key1 | value1 |
18 | key2 | value2 |
30 | key150 | value150 |
I want to delete all elements with keys from the second table.
I tried JSON_ARRAYAGG()
but JSON_REMOVE()
requires the "path[, path]"-structure with keys according to docs.
UPDATE `table1` as `t1`
SET `t1`.`data` = JSON_REMOVE(`t1`.`data`, (
SELECT JSON_ARRAYAGG(`key`)
FROM `table2` as `t2`
)
);
I haven't found examples or docs about json paths from subquery rows. How can I transform rows or maybe array values from returned rows to correct paths?
A result I expect after the query:
id | data |
---|---|
1 | {"key10": "value10", "key100": "value100"} |
20 | {"key18": "value18"} |
57 | {"key5": "value5", "key10": "value10"} |