Question

I am fetching data using the Discogs API and I wanted to restrict the JSON to only records that have format as 'CD' or 'Album' or 'Maxi-Single'. I am not sure how to filter fetched data since Discogs doesnt not have a easier way to filter this from the initial request itself. You'll notice that the JSON URL mentioned below has 'DVD' and 'VHS' that I would like to omit. Any idea/clues how I can filter this after fetching is done?

JSON URL: http://api.discogs.com/database/search?sort=year&sort_order=asc&artist=Britney+Spears&track=Crazy+Stop+Remix&type=master

Python

url = 'http://api.discogs.com/database/search?sort=year&sort_order=asc&artist=Britney+Spears&track=Crazy+Stop+Remix&type=master'
request = urllib2.Request(url)
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_object = json.load(response)
if json_object['results'] == []:
    print 'No results found'
else:
    json_object_results_by_year=[x for x in json_object['results'] if 'year' in x]
    json_sorted_results = sorted(json_object_results_by_year,key=lambda x:x['year'])
    #print json_sorted_results
Was it helpful?

Solution

wanted = {u'CD',u'Album',u'Maxi-Single'}
[i for i in json_object[u'results'] if any(w in wanted for w in i[u'format'])]

or

[i for i in json_object[u'results'] if any(w in i[u'format'] for w in wanted)]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top