Prem Minister Prem Minister - 1 month ago 11
JSON Question

Python JSON not fetching data from urllib

I am trying to fetch JSON data using the discogs API, but the code doesnt seem to give the output I see when I paste the data in a browser as raw. Kindly let me know what I am doing wrong here as I am new to Python. I was looking to get the output for title, images. Thanks!

Python:

import urllib
import urllib2
import json
url = 'http://api.discogs.com/masters/66271'
request = urllib2.Request('http://api.discogs.com/masters/66271')
request.add_header('User-Agent','Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)')
request.add_header('Content-Type','application/json')
response = urllib2.urlopen(request)
json_raw= response.readlines()
json_object = json.loads(json_raw[0])
print json_object
for row in json_object:
print row
print row['title']


wrong output:

styles
genres
videos
title
main_release
main_release_url
uri
artists
versions_url
year
images
resource_url
tracklist
id
data_quality

Answer

You should not use .readlines(). Let the json library do the reading:

response = urllib2.urlopen(request)
json_object = json.load(response)

Note the function name, .load(), no s at the end.

The object returned is a dictionary; each of the strings you see is a key into that dictionary. You need to specify what titles you'd want; the tracklist and videos entries both have a list of items with titles; here is how you'd print the tracklist entry titles:

for track in json_object['tracklist']:
    print track['title']

Which prints:

HIStory Begins
Billie Jean
The Way You Make Me Feel
Black Or White
Rock With You
She's Out Of My Life
Bad
I Just Can't Stop Loving You
Man In The Mirror
Thriller
Beat It
The Girl Is Mine
Remember The Time
Don't Stop 'Til You Get Enough
Wanna Be Startin' Somethin'
Heal The World
HIStory Continues
Scream
They Don't Care About Us
Stranger In Moscow
This Time Around
Earth Song
D.S.
Money
Come Together
You Are Not Alone
Childhood (Theme From "Free Willy 2")
Tabloid Junkie
2 Bad
History
Little Susie
Smile