Json KeyError with json.loads

JSON seems to have hiccuped the following statements:

{"delete":{"status":{"id":12600579001,"user_id":55389449}}} 

code snippet:

 temp = json.loads(line) text = temp['text'] 

I get the following error output when the above code snippet encounters lines similar to the above JSON dictionary:

 text = temp['text'] KeyError: 'text' 

Is it because there is no "text" key in the line, or because "delete" is not in the dictionary?

+7
json python
source share
8 answers

Is it because there is no "text" key in the line, or because "delete" is not in the dictionary?

This is because there is no β€œtext" key. If you print temp or check if the key 'text' in the resulting Python dictionary, you will notice that there is no key named 'text' . In fact, temp has only one key: 'delete' . The dictionary referenced by 'delete' contains one key 'status' , which contains another dictionary with two keys: 'user_id' and 'id' .

In other words, your structure is as follows:

 { "delete" : { "status" : { "id" : 12600579001, "user_id" : 55389449 } } } 

As you can see, there is no text key anywhere.

Alternatively, you can check it yourself:

 >>> 'text' in temp False >>> 'delete' in temp True 
+4
source share

This seems to be happening because the β€œtext” is missing there. Perhaps you could use something like

 'text' in temp 

to check if "text" exists before trying to use it.

Edit:

I gave the example indicated in the comment and added an if / elif / else block to it.

 #! /usr/bin/python import sys import json f = open(sys.argv[1]) for line in f: j = json.loads(line) try: if 'text' in j: print 'TEXT: ', j['text'] elif 'delete' in j: print 'DELETE: ', j['delete'] else: print 'Everything: ', j except: print "EXCEPTION: ", j 

Example of fragment No. 1:

{u'favorited ': False, u'contributors': None, u'truncated ': False, u'text': ---- snip ----}

Example of fragment No. 2:

{u'delete ': {u'status': {u'user_id ': 55389449, u'id': 12600579001L}}}

+3
source share

From the published snippet, it seems that temp should have only one element with the key "delete" . You don't have the 'text' key, so I'm not sure what to look for temp['text'] .

+2
source share

Try this to see the problem in detail:

 import json line = '{"delete":{"status":{"id":12600579001,"user_id":55389449}}}' print 'line:', line temp = json.loads(line) print 'temp:', json.dumps(temp, indent=4) print 'keys in temp:', temp.keys() 

What generates this output:

 line: {"delete":{"status":{"id":12600579001,"user_id":55389449}}} temp: { "delete": { "status": { "user_id": 55389449, "id": 12600579001 } } } keys in temp: [u'delete'] 

The only key in temp dict is "delete". Thus temp['text'] throws a KeyError.

+2
source share

Why not put this between the first and second lines:

 print temp 
+1
source share

try to do it like this:

  temp = json.load(line) for lines in temp text = lines['text'] 
+1
source share

Thanks everyone for the suggestions. The heart of the problem was that Twitter json format has a dictionary in the dictionary. The solution includes a double index to get the variables that I need to check.

0
source share
 #!/usr/bin/env python import sys import json from pprint import pprint json_file=sys.argv[1] json_data=open(json_file) j = json.load(json_data) def main(): for attribute_key in j['root_attribute']: try: print attribute_key['name'], attribute_key['status'], attribute_key['text'] except KeyError: pass if __name__ == "__main__": main() 
0
source share

All Articles