Home > Software engineering >  how to get a json that varais each request by it's number
how to get a json that varais each request by it's number

Time:02-17

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"]
  • I don't know where I can use these IDs to help extract the stories URL
  • I don't know how to use it to get the media URLs cause it changes with the number of stories also if there is another way to extract it, it may solve my problem
  • the "url" key is not unique it's used in other values

CodePudding user response:

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))
  • Related