0

I have a json file that look like this: I will have to extract the events eg. 'APP_STARTED' 'ORIENTATION' etc

{u'ParamElement_ReceivedTime': u'2012-11-02-00-05-31-748', 
 u'ParamElement_Name': u'LOG_CONTENT', 
 u'ParamElement_Info_0': 
 {u'dict': 
 {u'Events_list': [
 {
 u'Event': u'APP_STARTED', 
 u'time': u'2012-11-01 20:00:59.565 -0400'}, 
 {
 u'time': u'2012-11-01 20:01:01.168 -0400', 
 u'Event': u'ORIENTATION', 
 u'Orientation': u'Portrait'}, 
 {u'Event': u'CLIENT_RESULT_RECEIVED', 
 u'time': u'2012-11-01 20:01:15.927 -0400'}, 
 {u'Prev_SessionID': u'802911CC329E47139B61B58E21BF2FFF', 
 u'Prev_TransactionID': u'2', 
 u'Tab_Index': u'5', 
 u'time': u'2012-11-01 20:01:15.941 -0400', 
 u'Event': u'RESOLVED_TAB', 
 u'Accuracy': u'5.000000'}, 
 {u'Prev_TransactionID': u'2', 
 u'Prev_SessionID': u'802911CC329E47139B61B58E21BF2FFF', 
 u'Event': u'CLIENT_RESULT_RECEIVED', 
 u'time': u'2012-11-01 20:01:16.568 -0400'}
 }

The whole thing is stored in a variable called event_dict. I have a code that looks like:

if event_dict:
 if 'dict' in event_dict['ParamElement_Info_0']:
 if 'el' in event_dict['ParamElement_Info_0']['dict']:
 if 'e' in event_dict['ParamElement_Info_0']['dict']['el']:
 print e['Event'] 

What could be the mistake?

David Robinson
78.8k16 gold badges172 silver badges189 bronze badges
asked Mar 13, 2013 at 16:33
2
  • What mistake? What error or incorrect behavior are you getting? Commented Mar 13, 2013 at 16:34
  • no error. I tried to print dict and it is empty. Commented Mar 13, 2013 at 16:35

3 Answers 3

3

Python approach is Ask forgiveness, not permission, and it is easier and better to use try-catch blocks instead of condition checks unless condition fail must be handled separately.

try:
 event = event_dict['ParamElement_Info_0']['dict']['Events_list']
except Exception, e:
 log('Opsss, incorrect data format: %s' % e.message)

In that way, you can see your errors easily.

answered Mar 13, 2013 at 16:44
Sign up to request clarification or add additional context in comments.

Comments

2

You never define the variable e: your last line should be a loop, not a conditional like the earlier lines:

for e in event_dict['ParamElement_Info_0']['dict']['el']:
 print e

Also, I think you say "el" when you would need to say "Events_list", making your corrected code:

if event_dict:
 if 'dict' in event_dict['ParamElement_Info_0']:
 if 'Events_list' in event_dict['ParamElement_Info_0']['dict']:
 for e in event_dict['ParamElement_Info_0']['dict']['Events_list']:
 print e
answered Mar 13, 2013 at 16:35

4 Comments

Yes i use el for Events_list
What do you mean you "use el"? The name of the string in your dictionary is Events_list. You are checking whether that string is in your dictionary. You can't check for an entirely different string. Did you mean for it to be a for loop, like for el in event_dict...?
Sorry, I was meaning for el in event_dict... I did not change it back to event_list. Thanks for the pointer.
This prints 'Event' and its value.
2

There is no 'el' element in your dictionary. When you write a for A in B you are creating a variable A to hold the contents of the iterable B. What you are doing is saying, if the key 'el' is in my dictionary... which it isn't. But Events_list is as @David points out.

Here is what may be an easier approach.

def item_getter(struct, key):
 parts = key.split('.', 1)
 if len(parts) > 1:
 key_part, rest_part = parts
 return item_getter(struct.get(key_part, {}), rest_part)
 return struct.get(key, None)
items = item_getter(event_dict, "ParamElement_Info_0.dict.Events_list")
events = [item.get('Event', 'No Event') for item in items]
print events

OUTPUT

[u'APP_STARTED', u'ORIENTATION', u'CLIENT_RESULT_RECEIVED', u'RESOLVED_TAB', u'CLIENT_RESULT_RECEIVED']
answered Mar 13, 2013 at 16:47

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.