Kukula Mula
Kukula Mula

Reputation: 1869

Django deserialize JSON request with nested dictionary

I have the following Python structure I got from parsing the JSON request with the following command: request_data=json.loads(request.POST['request'])

{
    u'userID': u'123', 
    u'actions': {
        u'1': {u'notes': u'actionID= 35', u'actionType': u'7', u'currentTime': u'26/08/14'}, 
        u'0': {u'notes': u'bla bla', u'actionType': u'2', u'currentTime': u'26/08/14'}, 
        u'3': {u'notes': u'actionID= 31', u'actionType': u'7', u'currentTime': u'26/08/14'},  
        u'2': {u'notes': u'actionID= 14', u'actionType': u'7', u'currentTime': u'26/08/14'}, 
        u'5': {u'notes': u'actionID= 12', u'actionType': u'7', u'currentTime': u'26/08/14'}
    }
}

when i try looping on it how can I get to the notes and action type

counter=0
for key in user_actions:
   value=user_actions[str(counter)]
   #how can I extract notes out of value???                  
   counter = counter +1

I've tried:

It makes no sense to me cause if I print value i get the inner dictionary {u'notes': u'actionID= 35', u'actionType': u'7', u'currentTime': u'26/08/14'} but using the same logic to extract the inner dictionary values does not work

Upvotes: 0

Views: 565

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295403

for k, v in user_actions.iteritems():
    print v['notes']

Upvotes: 2

Related Questions