-1

I have the following JSON, I want to be able to get the value for a certain key and update it accordingly.

So for example take a look at the key 'a' in the first JSON object, I want to be able to get its value '2' and then update the value for 'a' to whatever I want. It is important I can work with the value, incase I want to reformat date/time for example and update the key.

All help appreciated

x = "test" : [{
       "a":"2",
       "b":"12",
       "c":"24",
       "d":"223",
       "e":"23",
       },
       {"a":"22",
       "x":"24",
       "c":"25",
       "d":"21",
       "e":"25",
       },
       {"a":"12",
       "y":"23",
       "c":"25",
       "d":"23",
       "e":"21",
       }],
mohsan123
  • 91
  • 2
  • 11

1 Answers1

0

You could do this.

keyFor = 'a'
#get value
aVal = x['test'][0][keyFor]
#change value
aVal = int(aVal) + 2
#substitute in x
x['test'][0][keyFor] = aVal

print(x)
Buddy Bob
  • 5,829
  • 1
  • 13
  • 44