Hossam Al-din Hassan
Hossam Al-din Hassan

Reputation: 51

how to get a json that varais each request by it's number

I made a request to Instagram v1 API it gives back the response in JSON like this The JSON data on pastebin.com

I noticed that I can get the number of IDs and the IDs by :

IDs = response['reels'][ide]["media_ids"]
count=response['reels'][ide]["media_count"]

Upvotes: 0

Views: 60

Answers (1)

Adon Bilivit
Adon Bilivit

Reputation: 27424

Assuming the "media URLs" are the values associated with a key "url" then you can just do this:

import json

def print_url(jdata):
    if isinstance(jdata, list):
        for v in jdata:
            print_url(v)
    elif isinstance(jdata, dict):
        if (url := jdata.get('url')):
            print(url)
        else:
            print_url(list(jdata.values()))


with open('instagram.json', encoding='utf-8') as data:
    print_url(json.load(data))

Upvotes: 1

Related Questions