nolan
nolan

Reputation: 63

Iterating specific values from a json response with Python

I am receiving a python dict that has the following format via curl.

json = connect(header, URL)

python dict

I am trying to iterate/get the different "number" values with the following code:

for i in json['mail']['number']: 

    print (str(i).replace('"',''))

but it does not work. Any ideas?

Upvotes: 0

Views: 25

Answers (1)

rauberdaniel
rauberdaniel

Reputation: 1033

You're iterating over the wrong object, try this:

for i in json['mail']:
    print(int(i['number']))

Please also note: naming a variable json could be very bad because most likely you are using the json package as well and this will lead to naming conflicts.

Upvotes: 1

Related Questions