cloudy_weather
cloudy_weather

Reputation: 2997

Replace the value of an item in a JSON collection in Python

I have the following collection (response from http request):

[{'ParameterKey': 'AdminCCIDRBlock', 'ParameterValue': '10.10.196.0/23'}, 
 {'ParameterKey': 'MyParameter', 'ParameterValue': 'true'}]

I need to send the same collection in another request but I have to change the value of MyParameter to false. So the collection would become:

[{'ParameterKey': 'AdminCCIDRBlock', 'ParameterValue': '10.10.196.0/23'}, 
 {'ParameterKey': 'MyParameter', 'ParameterValue': 'false'}]

How can I do that nicely in Python?

Upvotes: 0

Views: 72

Answers (1)

Yevhen Kuzmovych
Yevhen Kuzmovych

Reputation: 12140

I think you would need to traverse the whole list anyway, so:

for d in collection:
    if d['ParameterKey'] == 'MyParameter':
        d['ParameterValue'] = 'false'

Upvotes: 2

Related Questions