0

I have the following data get by JSON:

EVENT = {
         'eventid': '11828346', 
         'acknowledges': [{'alias': 'user1', 'name': 'userXYZ'}], 
         'objectid': '25946', 
         'clock': '1444051689', 
         'object': '0', 
         'acknowledged': '0', 
         'value': '0'
         }

So, to get what I want from this dictionary:

    for t in EVENT:
        TRIGGERID = t['objectid']
        HOUR = t['clock']
        ACK = t['acknowledged']

This part is ok, but how can I get the data from the inner dictionary?

[{'alias': 'user1', 'name': 'userXYZ'}]
Alvaro Silvino
  • 9,441
  • 12
  • 52
  • 80
Joao Vitorino
  • 2,976
  • 3
  • 26
  • 55

1 Answers1

4

The dictionary is stored inside a list (for some strange reason) so you can do

inner_dict = EVENT['acknowledged'][0]

to get the inner dictionary and then select from it like

inner_dict['alias'] 
JoeCondron
  • 8,546
  • 3
  • 27
  • 28
  • A dictionary is a key-value store, I can see this in all data (eventid:11828346, object:0 ...), so why you tell that is a list? If I said some nonsense, sorry, I started to learn python few weeks ago. – Joao Vitorino Oct 09 '15 at 19:59
  • 1
    No, I wasn't putting you down. `EVENT` is a dictionary, that is correct. But the value corresponding to the key `'acknowledges'` is a list, which you can see this from the brackets `[]`. This list has only one entry which is a dictionary, denoted by the braces `{}`. So what you have is a dictionary in a list in a dictionary, which seemed strange to me. I know you didn't create the data like that but read it from json. To select from a list you use integer indices, hence the `[0]` in my answer, which selects the first element, the dictionary, from it. – JoeCondron Oct 09 '15 at 20:34