0

My JSON file is

{ "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

My python code is as follows

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()

When i try to run the file, it gives me following error

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]

Can somebody help me and tell how do i resolve it?

9
  • 2
    This is an decoding problem. Do you know if the encoding of the file is UTF-8? If not try UTF-16 perhaps: io.open('data.json', 'r', encoding='utf-16'). I can't say for sure of course without having access to the file. Commented Feb 17, 2014 at 8:17
  • I am using Python 2.7. So i think encoding argument is not supported there Commented Feb 17, 2014 at 8:23
  • Import the io module and use io.open if you need to specify the encoding. See docs.python.org/2/library/io.html (Supported since 2.6 I believe.) Commented Feb 17, 2014 at 8:25
  • Done. It is giving me this error UnicodeDecodeError: 'utf16' codec can't decode bytes in position 298662-298663: illegal UTF-16 surrogate Commented Feb 17, 2014 at 8:28
  • Looks like it's not UTF-16 either. Do you know the encoding of the file? Where did you get it from? Can you inspect it to find out its encoding? Commented Feb 17, 2014 at 8:30

0

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.