I have downloaded a .json file from this web page and converted into a dictionary with the following commands:
import urllib.request, json
with urllib.request.urlopen("https://www.bcusu.com/svc/voting/stats/election/paramstats/109?groupIds=1,12,7,3,6&sortBy=itemname&sortDirection=ascending") as url:
data = json.loads(url.read().decode())
#print(data)
My final goal is to convert my data
, which is a dictionary into a pandas data frame. The main thing is that the data
dictionary is nested and, to complicate things a bit further, there is a single column (Groups
) which is nested.
I have found this solution which does the job for a 'uniformly' nested dictionary which looks like the following:
user_dict = {12: {'Category 1': {'att_1': 1, 'att_2': 'whatever'},
'Category 2': {'att_1': 23, 'att_2': 'another'}},
15: {'Category 1': {'att_1': 10, 'att_2': 'foo'},
'Category 2': {'att_1': 30, 'att_2': 'bar'}}}
By 'uniformly nested' I mean that the outer and inner keys in the dataframe above have all the same number of keys: 12
and 15
have both two keys Category 1
and Category 2
, which, finally, also have two keys att 1
and att 2
, which is not the case in my data
.