0

I am trying to parse a json file which looks something like below:

{ "results": [ 
{ "ID": "63768E9B-1D66-486A-BCDD-D3991EAFBE94", 
"Dt": "2013-08-03T13:01:26.901Z", 
"Dt_u": "2013-08-03T13:01:26.901Z", 
"obj": "enppXhI7TS" 
}, 
{ 
"ID": "63768E9B-1D66-486A-BCDD-D3991EAFBE94", 
"Dt": "2013-08-03T16:17:33.280Z",
"Dt_u": "2013-08-03T16:17:33.280Z",
"obj": "79J5z6y2UR" 
}, 
{ 
"ID": "F8B1B9FB-7BCD-47DF-89BD-241440BB6270",
"Dt": "2013-08-06T00:23:43.562Z", 
"obj": "Xf75BFtx4O",
"gender": 2,
"language": "en"
}]}

There are many more entries in the file

Now when i try to load this file using JSON Parser in python, it gives me

Traceback (most recent call last):
  File "E:\test.py", line 8, in <module>
    data = json.dumps(json_data)
  File "C:\python27\lib\json\__init__.py", line 243, in dumps
    return _default_encoder.encode(obj)
File "C:\python27\lib\json\encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
File "C:\python27\lib\json\encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
File "C:\python27\lib\json\encoder.py", line 184, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <open file 'data.json', mode 'r' at 0x00000000022E6540> is not JSON  
serializable

[Finished in 0.9s with exit code 1]

My code is

import json
from pprint import pprint


json_data=open('data.json','r')

data = json.dumps(json_data)
jsondata = data["results"]

for item in jsondata:
name = item.get("ID")
json_data.close()

<<<<<<>>>>>>>>>>

import json
from pprint import pprint


json_data=open('data.json','r')

data = json.load(json_data)
jsondata = data["results"]

for item in jsondata:
name = item.get("ID")
json_data.close()

Error that it gives now -->

Traceback (most recent call last):
  File "E:\test.py", line 7, in <module>
    data = json.load(json_data)
  File "C:\python27\lib\json\__init__.py", line 290, in load
    **kw)
  File "C:\python27\lib\json\__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "C:\python27\lib\json\decoder.py", line 365, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\python27\lib\json\decoder.py", line 381, in raw_decode
   obj, end = self.scan_once(s, idx)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xc2 in position 2: invalid    continuation byte
[Finished in 0.2s with exit code 1]

I know it is related to some Unicode data. but how do i identify and resolve it?

4
  • What JSON parser are you talking about? Commented Feb 17, 2014 at 7:46
  • You missed the comma after "obj": "Xf75BFtx4O", is that copy-paste error? Commented Feb 17, 2014 at 7:46
  • @msvalkon - Updated with code. Commented Feb 17, 2014 at 7:52
  • @user3 - Yes. Updated accordingly Commented Feb 17, 2014 at 7:52

3 Answers 3

1

You're trying to dump a string that is a file that you are reading from. If that sentence doesn't make sense to you that's because the underlying operation is nonsensical. Try json.load() instead.

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

3 Comments

I tried that. But now it is giving me UnicodeDecodeError. Details updated in question
If you have a new question then open a new question.
0

Firstly here is syntax error in the data your pasted. Missing one comma in the end of line 17.

Secondly you need to call json.load(json_data) in order to load json from file.

Comments

0

First,there are no keys named seniorID,second you need use json.load(json_data)

1 Comment

Updated key and json.load

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.