Reputation: 589
In Python 3-x, consider you have an array in JSON syntax:
members = '''[
{
"name" : "Amber",
"age" : 5
},
{
"name" : "Becky",
"age" : 4
}
]'''
How do you get the value for age
where the name
is Amber
? (The answer should be 5
).
Upvotes: 0
Views: 4887
Reputation: 4744
variable members look like a string so first change string to json object and search what you want.
members = '''[
{
"name" : "Amber",
"age" : 5
},
{
"name" : "Becky",
"age" : 4
}
]'''
import json
obj = json.loads(members) #Changing string to json
for some_variable in obj:
if some_variable['name'] == 'Amber':
print (some_variable['age']) # will print 5
Upvotes: 3