4

I have this nested dictionary that I get from an API.

response_body = \
{  
    u'access_token':u'SIF_HMACSHA256lxWT0K',
    u'expires_in':86000,
    u'name':u'Gandalf Grey',
    u'preferred_username':u'gandalf',
    u'ref_id':u'ab1d4237-edd7-4edd-934f-3486eac5c262',
    u'refresh_token':u'eyJhbGciOiJIUzI1N',
    u'roles':u'Instructor',
    u'sub':{  
        u'cn':u'Gandalf Grey',
        u'dc':u'7477',
        u'uid':u'gandalf',
        u'uniqueIdentifier':u'ab1d4237-edd7-4edd-934f-3486eac5c262'
    }
}

I used the following to convert it into a Python object:

class sample_token:
    def __init__(self, **response):
        self.__dict__.update(response)

and used it like this:

s = sample_token(**response_body)

After this, I can access the values using s.access_token, s.name etc. But the value of c.sub is also a dictionary. How can I get the values of the nested dictionary using this technique? i.e. s.sub.cn returns Gandalf Grey.

6
  • What's the reason you want to do that? Why not just response_body['sub']['cn'] ? Commented Jul 27, 2015 at 1:51
  • @MattDMo The data is already in python dict. This question doesn't have much to do with json itself really, just nested dictionaries. Commented Jul 27, 2015 at 1:51
  • @viraptor Well, I was trying out some alternatives. Commented Jul 27, 2015 at 1:52
  • @viraptor you're right, I was taking the OP at his word. It's late... Commented Jul 27, 2015 at 1:55
  • @MattDMo I have changed that as well. I hope its good now. Commented Jul 27, 2015 at 2:05

3 Answers 3

7

Maybe a recursive method like this -

>>> class sample_token:
...     def __init__(self, **response):
...         for k,v in response.items():
...             if isinstance(v,dict):
...                 self.__dict__[k] = sample_token(**v)
...             else:
...                 self.__dict__[k] = v
...
>>> s = sample_token(**response_body)
>>> s.sub
<__main__.sample_token object at 0x02CEA530>
>>> s.sub.cn
'Gandalf Grey'

We go over each key:value pair in the response, and if value is a dictionary we create a sample_token object for that and put that new object in the __dict__() .

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

Comments

1

You can iterate over all key/value pairs with response.items() and for each value which isinstance(value, dict), replace it with sample_token(**value).

Nothing will do the recursion automagically for you.

Comments

0

Once you've evaluated the expression in Python, it's not a JSON object anymore; it's a Python dict; the usual way to access entries is with the [] indexer notation, e.g.:

response_body['sub']['uid']
'gandalf'

If you must access it as an object rather than a dict, check out the answers in the question Convert Python dict to object?; the case of nested dicsts is covered in one of the later answers.

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.