I want to update parts of a JSON doc inside a MySQL database table.
The field name in the database: jdata
I do not want to set it like jdata = '{_whatever_}'
, because I would loose keys.
I need to preserve existing keys (e.g. key1
, key3
) and only set key2
to its new designated value 999
.
{
"calculation":{
"key1": 100,
"key2": 200,
"key3": 300
}
}
$a = json_encode(['key2' => 999], JSON_UNESCAPED_SLASHES);
$sql =
" UPDATE svtr.auftrag
SET jdata = JSON_SET(jdata, '$.calculation', '{$a}')
WHERE[...]
";
Using the full path like so:
JSON_SET(jdata, '$.calculation.key2', 999)
is no option here, since the original JSON doc is much more complex and I would have to iterate many keys. Think of it like having 30 key/value pairs and needing to update 29 of them.
Hope anyone can make sense of my ramblings ... Thanks in advance!