1

I am trying to sort JSON data by the keys's value (year) and then trying to only display items that are in format 'DVD', I tried many methods but am not alble to arrive at a clean solution. Can you please help me on how I can solve this? I am new to python, kindly pardon if this is too simple to ask here.

JSON:

{
    "pagination": {
        "per_page": 50,
        "pages": 1,
        "page": 1,
        "urls": {},
        "items": 8
    },
    "results": [{
        "style": ["RnB/Swing", "Ballad"],
        "thumb": "http://api.discogs.com/image/R-90-217365-1270055830.jpeg",
        "title": "Britney Spears - Oops!...I Did It Again",
        "country": "US",
        "format": ["CD", "Album"],
        "uri": "/Britney-Spears-OopsI-Did-It-Again/master/27443",
        "label": ["Jive"],
        "catno": "01241-41704-2",
        "year": "2000",
        "genre": ["Pop"],
        "resource_url": "http://api.discogs.com/masters/27443",
        "type": "master",
        "id": 27443
    }, {
        "thumb": "http://api.discogs.com/image/R-90-2029929-1259535792.jpeg",
        "title": "Britney Spears - The Singles Collection",
        "country": "UK",
        "format": ["CD", "Compilation"],
        "uri": "/Britney-Spears-The-Singles-Collection/master/203074",
        "label": ["Jive"],
        "catno": "88697 623422",
        "year": "2009",
        "genre": ["Pop"],
        "resource_url": "http://api.discogs.com/masters/203074",
        "type": "master",
        "id": 203074
    }, {
        "style": ["RnB/Swing"],
        "thumb": "http://api.discogs.com/image/R-90-1895804-1250792689.jpeg",
        "title": "Britney Spears - Greatest Hits: My Prerogative",
        "country": "US",
        "format": ["DVD", "Compilation", "NTSC"],
        "uri": "/Britney-Spears-Greatest-Hits-My-Prerogative/master/270902",
        "label": ["Jive", "Zomba Video"],
        "catno": "82876-65443-9A",
        "year": "2004",
        "genre": ["Electronic", "Pop"],
        "resource_url": "http://api.discogs.com/masters/270902",
        "type": "master",
        "id": 270902
    }, {
        "style": ["RnB/Swing", "Ballad"],
        "thumb": "http://api.discogs.com/image/R-90-2421214-1283141143.jpeg",
        "format": ["DVD", "DVD-Video", "NTSC", "Multichannel"],
        "country": "US",
        "barcode": ["0 1241-41784-9 7"],
        "uri": "/Britney-Spears-Live-From-Las-Vegas/master/271031",
        "label": ["Jive"],
        "catno": "01241-41784-9",
        "year": "2002",
        "genre": ["Electronic", "Hip Hop", "Pop"],
        "title": "Britney Spears - Live From Las Vegas",
        "resource_url": "http://api.discogs.com/masters/271031",
        "type": "master",
        "id": 271031
    }, {
        "style": ["Europop"],
        "thumb": "http://api.discogs.com/image/R-90-2410806-1283122933.jpeg",
        "format": ["DVD", "DVD-Video", "NTSC"],
        "country": "US",
        "barcode": ["0 1241-41704-9 1"],
        "uri": "/Britney-Spears-Live-And-More/master/270904",
        "label": ["Jive"],
        "catno": "01241-41704-9",
        "year": "2000",
        "genre": ["Pop"],
        "title": "Britney Spears - Live And More!",
        "resource_url": "http://api.discogs.com/masters/270904",
        "type": "master",
        "id": 270904
    }]
}

PYTHON:

url1 = "http://api.discogs.com/database/search?sort=year&sort_order="
url2 = "asc&artist=%22"+artist+"%22&track="
url3 = "%22"+song+"%22&format_exact=Album&type=master"
url = url1 + url2 + url3
print url
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_raw = response.readlines()
json_object = json.loads(json_raw[0])

for row in sorted(json_object['results']): #stuck here to sort on key's value...
    print '-----------------------------start------------------------------------------'
    print row['format']
    print row['id']
    print '-----------------------------end--------------------------------------------'
1

1 Answer 1

2

sorted accepts a key argument, which does just that:

from operator import itemgetter

for result in sorted(json_object['results'], key=itemgetter('year')):
    if 'DVD' in result['format']:
        continue
Sign up to request clarification or add additional context in comments.

4 Comments

@delnan: Completely forgot about that one. Thanks.
Thank you for the help, it seems like the data now is sorted by year (awesome) but then the data is not restricted to values containing 'DVD'
for row in sorted(json_object['results'], key=lambda result: result['year']): print '--------start--------' pprint.pprint(row) print '--------end--------' #print str(type) if 'DVD' not in row['format']: print row['title'] + '-' + row['year'] + '-' + row['genre'][0] + '-' + row['format'][0] + '-' + row['label'][0] + '-' + row['resource_url']
@PremMinister: My mistake, 'DVD' not in should be 'DVD' in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.