Reputation: 49
I am using an API from a website and I am struggling to collect the data i need from the JSON I receive. Here is an example of the JSON I have received:
{'success': True, 'tracks': {'data': {'12958571': {'position': 3, 'id': '12958571llwU', 'track': 'Are You With Me', 'bitrate': '320', 'text_id': None, 'count_listen': '2110', 'artist': 'Lost Frequencies feat Easton Corbin', 'server_id4': '0', 'lenght': '211'}, '12924079': {'position': 8, 'id': '12924079tZmc', 'track': 'Love Me Like You Do', 'bitrate': '320', 'text_id': None, 'count_listen': '1654', 'artist': 'Ellie Goulding', 'server_id4': '0', 'lenght': '233'}, '12241830': {'position': 12, 'id': '12241830KIyP', 'track': 'Blame', 'bitrate': 'VBR', 'text_id': None, 'count_listen': '1093', 'artist': 'Calvin Harris feat. John Newman', 'server_id4': 's20', 'lenght': '211'}, '1036820': {'position': 16, 'id': '1036820770C', 'track': 'LILY WAS HERE', 'bitrate': '256', 'text_id': None, 'count_listen': '307', 'artist': 'CANDY DULFER DAVE STEWART', 'server_id4': 's20', 'lenght': '253'}, '12441354': {'position': 14, 'id': '12441354CAj2', 'track': 'Love Who Loves You Back', 'bitrate': 'VBR', 'text_id': None, 'count_listen': '975', 'artist': 'Tokio Hotel', 'server_id4': '0', 'lenght': '229'}, '12697344': {'position': 13, 'id': '12697344Polh', 'track': 'Outside', 'bitrate': '160', 'text_id': None, 'count_listen': '1050', 'artist': 'Calvin Harris feat. Ellie Goulding', 'server_id4': '0', 'lenght': '226'}, '5738480': {'position': 5, 'id': '5738480NApX', 'track': 'Love Me Again', 'bitrate': '320', 'text_id': '18660', 'count_listen': '1830', 'artist': 'John Newman', 'server_id4': '0', 'lenght': '215'}, '12538432': {'position': 4, 'id': '12538432tnyz', 'track': 'Dangerous', 'bitrate': 'VBR', 'text_id': None, 'count_listen': '1916', 'artist': 'David Guetta (feat. Sam Martin)', 'server_id4': '0', 'lenght': '169'}, '12881624': {'position': 9, 'id': '12881624Igo4', 'track': 'The Good, The Bad And The Crazy', 'bitrate': '320', 'text_id': None, 'count_listen': '1374', 'artist': 'Imany, Filatov, Karas', 'server_id4': '0', 'lenght': '260'}, '6449584': {'position': 19, 'id': '6449584PwRx', 'track': 'Wrecking Ball', 'bitrate': '192', 'text_id': '18512', 'count_listen': '259', 'artist': 'Miley Cyrus', 'server_id4': '0', 'lenght': '223'}, '13361656': {'position': 11, 'id': '13361656Gr1A', 'track': 'Reality', 'bitrate': 'VBR', 'text_id': None, 'count_listen': '1131', 'artist': 'Lost Frequencies feat. Janieck Devy', 'server_id4': '0', 'lenght': '158'}, '6810440': {'position': 17, 'id': '6810440Uc4B', 'track': 'Royals', 'bitrate': '320', 'text_id': None, 'count_listen': '272', 'artist': 'Lorde', 'server_id4': '0', 'lenght': '192'}, '13241397': {'position': 7, 'id': '13241397v1h3', 'track': 'Ghost Town', 'bitrate': 'VBR', 'text_id': None, 'count_listen': '1668', 'artist': 'Adam Lambert', 'server_id4': '0', 'lenght': '208'}, '13482545': {'position': 1, 'id': '13482545fiqs', 'track': 'Supergirl', 'bitrate': '320', 'text_id': None, 'count_listen': '2431', 'artist': 'Anna Naklab, Younotus feat. Alle Farben', 'server_id4': '0', 'lenght': '212'}, '13154936': {'position': 2, 'id': '131549364jlY', 'track': 'Lean On', 'bitrate': 'VBR', 'text_id': None, 'count_listen': '2297', 'artist': 'Major Lazer x DJ Snake feat. MØ', 'server_id4': '0', 'lenght': '176'}, '13416218': {'position': 15, 'id': '134162185MCV', 'track': 'How Deep Is Your Love', 'bitrate': '320', 'text_id': None, 'count_listen': '922', 'artist': 'Calvin Harris & Disciples', 'server_id4': '0', 'lenght': '212'}, '8021072': {'position': 20, 'id': '8021072mjSx', 'track': 'Under Control', 'bitrate': '320', 'text_id': None, 'count_listen': '256', 'artist': 'Alesso feat. Calvin Harris & Hurts', 'server_id4': '0', 'lenght': '215'}, '12496862': {'position': 10, 'id': '124968620fP6', 'track': 'Imagination', 'bitrate': '320', 'text_id': None, 'count_listen': '1295', 'artist': 'Gorgon City feat. Katy Menditta', 'server_id4': '0', 'lenght': '387'}, '12701434': {'position': 6, 'id': '12701434zPSR', 'track': 'Tribute', 'bitrate': 'VBR', 'text_id': None, 'count_listen': '1801', 'artist': 'Homeless Man', 'server_id4': '0', 'lenght': '94'}, '13478793': {'position': 18, 'id': '13478793fu3c', 'track': 'Hello', 'bitrate': '320', 'text_id': None, 'count_listen': '268', 'artist': 'Adele', 'server_id4': '0', 'lenght': '295'}}, 'count': 1000}}
Sorry that the JSON is all in one line. Anyway for some reason the nesting here is completely disastrous. What I want is to have an array for each track that is represented. The tracks are nested in the data key but each track is not in an array (as the api claims) but is in fact part of a dictionary with the key being its id (i.e. '12958571'). However I struggle to see how I will collect the data I need (artist, track etc.) if the value of the keys will vary based on what response I get from the api. Is there a way that I can get all the data and convert it into an array even though I don't know what the value of the key will be?
edit: This is the pure valid JSON from the server:
{
"success": true,
"tracks": {
"count": 1000,
"data": {
"13482545": {
"id": "13482545fiqs",
"artist": "Anna Naklab, Younotus feat. Alle Farben",
"track": "Supergirl",
"lenght": "212",
"text_id": null,
"bitrate": "320",
"server_id4": "0",
"count_listen": "2466",
"position": 1
},
"13154936": {
"id": "131549364jlY",
"artist": "Major Lazer x DJ Snake feat. M\u00d8",
"track": "Lean On",
"lenght": "176",
"text_id": null,
"bitrate": "VBR",
"server_id4": "0",
"count_listen": "2335",
"position": 2
},
"12958571": {
"id": "12958571llwU",
"artist": "Lost Frequencies feat Easton Corbin",
"track": "Are You With Me",
"lenght": "211",
"text_id": null,
"bitrate": "320",
"server_id4": "0",
"count_listen": "2140",
"position": 3
},
"12538432": {
"id": "12538432tnyz",
"artist": "David Guetta (feat. Sam Martin)",
"track": "Dangerous",
"lenght": "169",
"text_id": null,
"bitrate": "VBR",
"server_id4": "0",
"count_listen": "1950",
"position": 4
},
"5738480": {
"id": "5738480NApX",
"artist": "John Newman",
"track": "Love Me Again",
"lenght": "215",
"text_id": "18660",
"bitrate": "320",
"server_id4": "0",
"count_listen": "1860",
"position": 5
},
"12701434": {
"id": "12701434zPSR",
"artist": "Homeless Man",
"track": "Tribute",
"lenght": "94",
"text_id": null,
"bitrate": "VBR",
"server_id4": "0",
"count_listen": "1834",
"position": 6
},
"13241397": {
"id": "13241397v1h3",
"artist": "Adam Lambert",
"track": "Ghost Town",
"lenght": "208",
"text_id": null,
"bitrate": "VBR",
"server_id4": "0",
"count_listen": "1696",
"position": 7
},
"12924079": {
"id": "12924079tZmc",
"artist": "Ellie Goulding",
"track": "Love Me Like You Do",
"lenght": "233",
"text_id": null,
"bitrate": "320",
"server_id4": "0",
"count_listen": "1680",
"position": 8
},
"12881624": {
"id": "12881624Igo4",
"artist": "Imany, Filatov, Karas",
"track": "The Good, The Bad And The Crazy",
"lenght": "260",
"text_id": null,
"bitrate": "320",
"server_id4": "0",
"count_listen": "1402",
"position": 9
},
"12496862": {
"id": "124968620fP6",
"artist": "Gorgon City feat. Katy Menditta",
"track": "Imagination",
"lenght": "387",
"text_id": null,
"bitrate": "320",
"server_id4": "0",
"count_listen": "1317",
"position": 10
},
"13361656": {
"id": "13361656Gr1A",
"artist": "Lost Frequencies feat. Janieck Devy",
"track": "Reality",
"lenght": "158",
"text_id": null,
"bitrate": "VBR",
"server_id4": "0",
"count_listen": "1146",
"position": 11
},
"12241830": {
"id": "12241830KIyP",
"artist": "Calvin Harris feat. John Newman",
"track": "Blame",
"lenght": "211",
"text_id": null,
"bitrate": "VBR",
"server_id4": "s20",
"count_listen": "1112",
"position": 12
},
"12697344": {
"id": "12697344Polh",
"artist": "Calvin Harris feat. Ellie Goulding",
"track": "Outside",
"lenght": "226",
"text_id": null,
"bitrate": "160",
"server_id4": "0",
"count_listen": "1071",
"position": 13
},
"12441354": {
"id": "12441354CAj2",
"artist": "Tokio Hotel",
"track": "Love Who Loves You Back",
"lenght": "229",
"text_id": null,
"bitrate": "VBR",
"server_id4": "0",
"count_listen": "990",
"position": 14
},
"13416218": {
"id": "134162185MCV",
"artist": "Calvin Harris & Disciples",
"track": "How Deep Is Your Love",
"lenght": "212",
"text_id": null,
"bitrate": "320",
"server_id4": "0",
"count_listen": "930",
"position": 15
},
"1036820": {
"id": "1036820770C",
"artist": "CANDY DULFER DAVE STEWART",
"track": "LILY WAS HERE",
"lenght": "253",
"text_id": null,
"bitrate": "256",
"server_id4": "s20",
"count_listen": "318",
"position": 16
},
"6810440": {
"id": "6810440Uc4B",
"artist": "Lorde",
"track": "Royals",
"lenght": "192",
"text_id": null,
"bitrate": "320",
"server_id4": "0",
"count_listen": "275",
"position": 17
},
"13478793": {
"id": "13478793fu3c",
"artist": "Adele",
"track": "Hello",
"lenght": "295",
"text_id": null,
"bitrate": "320",
"server_id4": "0",
"count_listen": "272",
"position": 18
},
"6449584": {
"id": "6449584PwRx",
"artist": "Miley Cyrus",
"track": "Wrecking Ball",
"lenght": "223",
"text_id": "18512",
"bitrate": "192",
"server_id4": "0",
"count_listen": "267",
"position": 19
},
"4473614": {
"id": "44736143Ee9",
"artist": "Eminem feat. Rihanna",
"track": "Love The Way You Lie",
"lenght": "263",
"text_id": "18072",
"bitrate": "192",
"server_id4": "0",
"count_listen": "260",
"position": 20
}
}
}}
Upvotes: 0
Views: 99
Reputation: 40723
Assume that the JSON object you get back is called jobject
, then here is a suggestion on how to access the information:
print('Success?', jobject['success'])
print('Number of tracks claimed:', jobject['tracks']['count'])
print('Number of actual tracks:', len(jobject['tracks']['data']))
for track_id, track_info in jobject['tracks']['data'].items():
print('Track:', track_info['track'])
print('Artist:', track_info['artist'])
print('---')
Output:
Success? True
Number of tracks claimed: 1000
Number of actual tracks: 20
Track: Royals
Artist: Lorde
---
Track: Dangerous
Artist: David Guetta (feat. Sam Martin)
---
Track: Love Who Loves You Back
Artist: Tokio Hotel
---
...
Upvotes: 2
Reputation: 149
First sanity check your JSON to ensure it's valid, i.e. use http://jsonlint.com
# coding=utf-8
#!/usr/local/bin/python
import json
jsonString = "{'success': True, ..." #your JSON string here
jdict = json.decoder.JSONDecoder().decode(jsonString)
Then jdict will contain a dictionary representing ALL the contents of the JSON, which you can access with e.g.
tracks = jdict['tracks']
You may want to manually structure and re-arrange these sub/dictionaries accordingly. You can easily merge two dictionaries in python e.g.
z = x.copy()
z.update(y)
Upvotes: 0