I am trying to update a dynamic json key value. But i am not able to the recursive function done. My JSON structure is
x = {
"payload": {
"name": "kabilan",
"address": {
"country": "value_to_change"
}
}
}
In the above json, path to "value_to_change" is ['payload']['address']['country'] which i store it as "payload.address.country" in db.
This nested json structure is dynamically created but i also know the key path. Please find the code that i have written to change it
y = "payload.address.country"
y1 = y.split('.')
for item in y1:
if item == y1[-1]:
x[item] = "india"
else:
x = x[item]
print(x)
This code returns
"country":"india"
. But i want the output as
{
"payload": {
"name": "kabilan",
"address": {
"country": "india"
}
}
}
I think i am missing recursive function here. But i am getting confused in it. Kindly help me to solve this. Thanks in advance