birgit
birgit

Reputation: 1129

Iterating over Instagram json

I received a JSON object as a response to an Instagram API call.

I want to access the URLs of all pictures in the response, so far I managed to access the first image only:

from pprint import pprint
with open('test.json') as data_file:
    mydata = json.load(data_file)
    print mydata["data"][0]["images"]["standard_resolution"]["url"]

I'm struggling how to iterate over mydata correctly.

The json looks roughly like:

{
"meta:":{}
"data":[
    {}
    {}
    {}
}

I get stuck at this nested loop:

for x in mydata["data"]:
   for y in x:
       print y

outputs

attribution
tags
user
comments
filter
images
link
location
created_time
users_in_photo
caption
type
id
likes 

Upvotes: 0

Views: 171

Answers (2)

Anand S Kumar
Anand S Kumar

Reputation: 91007

If your first image is accessed as -

mydata["data"][0]["images"]["standard_resolution"]["url"]

Then you should iterate over mydata["data"] , which is a list, using for loop and get each url from each dictionary in it.

Example -

with open('test.json') as data_file:
    mydata = json.load(data_file)
    for img in mydata["data"]:
        print img["images"]["standard_resolution"]["url"]

Upvotes: 1

taesu
taesu

Reputation: 4580

dataset = mydata['data']
for data in dataset:
   url = data['images']['standard_resolution']['url']

Upvotes: 2

Related Questions