phicon phicon - 4 months ago 30
JSON Question

Python Print all from json

I am able to use the following code to extract one of the artist of all the artists in the list.

print (data['topartists']['artist'][0]['name'])

Now i would like the print to return all the artists names.

I would assume using for:

for i in data:
print (data['topartists']['artist'][i]['name'])

However this results in the error:
TypeError: list indices must be integers, not unicode

What should i add to print all the artists?


You should iterate artists, not the data itself.

for i in data['topartists']['artist']:
    print (i['name'])