Home > Net >  How to find a specific key in a list of dictionaries - Python/GET request response JSON
How to find a specific key in a list of dictionaries - Python/GET request response JSON

Time:04-12

I am using Python Requests to make a GET request.
response = requests.get(MYURL, headers = headers) data = response.json()

In most cases, the response has an ideal structure: Sample (ideal structure) response: https://0bin.net/paste/QyD1XU3z#2WRDIBvq HJtS4CyNbdGL7HhU9S2sLK ZjQpTjA-sgE

When it is structured as above, I can store a few desired values as such: artist_name = data[0]['artist']['artistName'] foreign_id = data[0]['artist']['foreignArtistId']

In some cases, the response is a bit different, but still contains the data points I am after. But, they are not always in data[0]. They could be in data[1] or some other indice. Example of unideal structure: https://0bin.net/paste/Myc bkQV#rYfL156jjLuqfDJGY fj9OD4gKjJJXud5hILB61bqPC

MY QUESTION: How can I iterate through the various indices of data[] to find ['artist']['artistName'] and ['artist']['foreignArtistId']

I was thinking if I could use a variable like d to be an int, then use recursion to increase d = d 1 while searching for the two values I want, I could solve my problem. But, this is giving me issues/immense confusion.

CodePudding user response:

The list in data contains both albums and artists, you should check to see which type of data is contained in the current iteration. If it's an artist, then you should be able to extract the name and foreign artist id.

for d in data:
    if d.get("artist"):  # check for artist type
        print(d["artist"]["foreignArtistId"])
        print(d["artist"]["artistName"])

CodePudding user response:

Try this :

# This function will traverse every nested dict to find your given key and it will return value of that key :)
def findkeys(node, kv):
    if isinstance(node, list):
        for i in node:
            for x in findkeys(i, kv):
               yield x
    elif isinstance(node, dict):
        if kv in node:
            yield node[kv]
        for j in node.values():
            for x in findkeys(j, kv):
                yield x


data = [{},{}, ... ]

# Iterate over list and fetch name & id from each dict if present
for obj in data:
  names = list(findkeys(obj, "artistName"))
  ids = list(findkeys(obj, "foreignArtistId"))
  if len(name)>0:
    name = names[0]

  if len(id)>0:
    id = ids[0]

  print(name, id)

  • Related