Reputation: 1863
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?
Upvotes: 0
Views: 1418
Reputation: 283
First,there are no keys named seniorID,second you need use json.load(json_data)
Upvotes: 0
Reputation: 369
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.
Upvotes: 0
Reputation: 799520
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.
Upvotes: 1