0

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).

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

all is not a good word to use for a variable since it is a reserved keyword in python. Also you should break out of the loop after you got what you wanted.
I just want to give a quick answer so I didn't pay no attendion.Yes all is reserved word your are right .Thanks to remembered me :) @Error-SyntacticalRemorse

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.