Question

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
Was it helpful?

Solution

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

OTHER TIPS

Try this,

opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11')]
http_handle = opener.open(url)
jsonContent = http_handle.read()
http_handle.close()

import json
data = json.load(jsonContent)
data["videos"]["title"]//will return the title
data["videos"]["images"]["uri"]//will return the image url

The array you are getting from the JSON looks like this:

{
    u 'images': [{
        u 'uri': u 'http://api.discogs.com/image/R-446273-1356211752-6937.jpeg',
    }, {
        u 'uri': u 'http://api.discogs.com/image/R-446273-1239130419.jpeg',
    }, {
        u 'uri': u 'http://api.discogs.com/image/R-446273-1239130427.jpeg',
    }, {
    ...
}

There is an "images" entry with many entries in them. These are all sub-entries of "images", "images" itself has no "uri" attribute that you can fetch.

You have to iterate through all the images, same with the track titles. Replace your for row in json_object loop with this:

for row in json_object["images"]:
    print row['uri']
for row in json_object["tracklist"]:
    print row['title']
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top