Created
August 7, 2014 11:33
-
-
Save sammachin/68769b41166245a7e593 to your computer and use it in GitHub Desktop.
Parse JSON STAT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#First we create an empty dict object | |
data = {} | |
#We need to specify the dataset name | |
dataset = "QS104EW" | |
# We get the actaul observation values from the JSON-STAT as a list | |
values = obj[dataset]['value'] | |
# Then we get the index of the observations and its asccociated categories as a dict | |
index = obj[dataset]['dimension'][obj[dataset]['dimension']['id'][1]]['category']['index'] | |
#and finally the labels for the categories as another dict | |
labels = obj[dataset]['dimension'][obj[dataset]['dimension']['id'][1]]['category']['label'] | |
for l in labels: #Now we can iterate through the labels | |
num = index[l] # get the position in the values dict of the specific label | |
count = values[str(num)] #get that value from the values list | |
data[labels[l]] = count #create a new object in data dict for the value with the label as its name | |
print data #All done! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment