Parsing JSON Nested Dictionary Using Python
Solution 1:
To understand how your json is set up, it's easier to break it down. Let's look at the first dictionary's keys, and remove the values.
json = {"items": [], "links": {}}
You have a dictionary with two keys and two values. All three of the variables you are looking for (id, self, name) are in the first key, "items". So let's dive deeper.
json["items"] = [{'links': {'self': 'https://www.google.com'}, 'name': 'beast', 'type': 'Device', 'id': '12345'}]
Now you have a list containing a dictionary with the values you are looking for, so let's enter the first and only value of the list containing the next dictionary.
json["items"][0] = {'links': {'self': 'https://www.google.com'}, 'id': '12345', 'type': 'Device', 'name': 'beast'}
Finally we have the dictionary with the values are looking for, so you can use this code to find name and id.
json["items"][0]["name"] = beast
json["items"][0]["id"] = 12345
The self variable is hidden one dictionary deeper so we have to delve into the links key.
json["items"][0]["links"]["self"] = http://google.com
Now you have all of your values, you just need to follow through all the lists and dictionaries to get the value you want.
Solution 2:
You can write a function, that will get the values you need:
def dict_get(x,key,here=None):
x = x.copy()
if here is None: here = []
if x.get(key):
here.append(x.get(key))
x.pop(key)
else:
for i,j in x.items():
if isinstance(x[i],list): dict_get(x[i][0],key,here)
if isinstance(x[i],dict): dict_get(x[i],key,here)
return here
dict_get(a,'id')
['12345']
dict_get(a,'self')
['https://www.google.com', 'https://www.google.com']
dict_get(a,'name')
['beast']
You can as well call any key that you want:
data
a = {
"items": [
{
"id": "12345",
"links": {
"self": "https://www.google.com"
},
"name": "beast",
"type": "Device"
}
],
"links": {
"self": "https://www.google.com"
},
"paging": {
"count": 1,
"limit": 1,
"offset": 0,
"pages": 1
}
}
Solution 3:
Your json basically contains lists inside it. Jsons are accessed as key value pairs and lists are accessed using indexes.
json_object['items'][0]['id']
The above statement should give you the id. We are accessing the key items, which contains a list. We have [0] because this list contains only one element (which is again a key value pair).
The same approach is followed for self and name
json_object['links']['self']
json_object['items'][0]['links']['self']
json_object['items'][0]['name']
The difference between accessing the two different 'self' bring that one is enclosed in a list hence we need to get into the list and the other one is a dictionary with key 'self' which is inside a dictionary 'links'
Solution 4:
It helps to write the dictionary indented, to better view its nesting:
mydict = {
'items': [
{
'id': '12345',
'links': {'self': 'https://www.google.com'},
'name': 'beast',
'type': 'Device'
}
],
'links': {'self': 'https://www.google.com'},
'paging': {
'count': 1,
'limit': 1,
'offset': 0,
'pages': 1
}
}
Now you can extract what you want:
If I want to get "id", "self", "name",
print(mydict['items'][0]['id'])
print(mydict['items'][0]['links']['self'])
print(mydict['links']['self'])
print(mydict['items'][0]['name'])
Post a Comment for "Parsing JSON Nested Dictionary Using Python"