28

I have the following nested Dictionary in JSON. If I want to get "id", "self", "name", how should I parse it using a Python Program.

{
  "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
  }
}
3
  • which self? both of them? Commented Aug 10, 2018 at 14:23
  • @Onyambu yes. for both of them. Commented Aug 10, 2018 at 14:24
  • Not exactly the same question but should point you in the right direction Parsing values from a JSON file Commented Aug 10, 2018 at 14:24

4 Answers 4

35

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you so much!
No problem, glad I could help
thank you so much, this example is about 1 item and one links keys. How can I apply that to get all items and links if there is more than one?
Very very helpful - something that actually makes sense!
Oh, man, I was missing the fact that there was a list embedded in my json, and that I needed '[0]'. Thanks!
3

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
  }
}

3 Comments

What is "a" in dict_get(a,id)?
When I call the function, there is no ouptut that I get. How did you get [''12345"]?
@IamL just run the function again and I get exactly what I have posted. Can you copy paste the code again?
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'

Comments

2

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'])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.