I have a json and I need to create a list of list like:
lista = [['334507', 'XXX', '334507', 36.07, 3985499.0],
['271018','YYY', '271007', 23.11, 1335150.0]]
all_sites = {
u'displayValue': {
u'siteId': {u'334507': u'XXX', u'271018': u'YYY'}
},
u'rows': [[u'334507', 36.07, 3985499.0],
[u'271018', 23.11, 1335150.0]],
u'alert': None,
u'columns': [u'siteId', u'revenue', u'paidImpressions'],
u'currency': u'USD'
}
I tried with something like this:
sites = all_sites['displayValue']['siteId'].items()
sites_data = all_sites['rows']
data = []
for item in sites:
data.append(list(item))
for item in sites_data:
data.append(item)
But how can I merge the list with the first item of the list??
all_sites['displayValue']['siteId']is of type dictionary and to access both key and value you need to usefor key,value in all_sites['displayValue']['siteId'].items(). But your data is stored in another variable:all_sites['displayValue']['rows']. Your attempt to fetch all data is incomplete.json_variable should've been calledall_sites.rowsa dictionary instead where the key would be your siteId. Otherwise, you'd have to iterate though all indices until you find the index where the element (sub-array) has your siteId as the first index.