I am trying to update a nested JSON which is given as an input, e.g.:
{
"doc": {
"a1": {
"b1": 1,
"b2": [
"one",
"two"
]
},
"a2": "xyz"
}
}
The JSON structure is not known.
Field to update is an input string eg: "doc.a2" or "doc.a1.b1" or "doc.a1.b2"
In addition to the path, the old value and a new value is also given as input. If the value in the path matches the old value, I want to update it with new value.
I tried with eval()
and seems possible like
if path is "doc.a1.b1", then eval(doc['a1']['b1']='new value')
;
But using eval
may lead to security issues. Is there a better way to achieve this?