Python Forum

Full Version: can't parse json file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have below code:
import os
import datetime
import json


def prt_list(vdata, vtype,vcol):
   print("Here is listing of {}:".format(vtype))
   for item in vdata:
     print (item[vcol])
 

wk_dir = '1139'
file_name = 'cust.json'

v_path=os.path.join('c:\\dell',wk_dir)
v_file=os.path.join('c:\\dell',wk_dir,file_name)

v_files=os.listdir(v_path)
for f in v_files:
  print(f)
print("\n")
m_file=open(v_file,'r')

m_data=json.load(m_file)
print(m_data)

prt_list(m_data,'customers','name')

m_file.close()
json file cust.json below:
Output:
[ { "id":1, "name": "jame" "email": "[email protected]" }, { "id":2, "name": "Apple" "email": "[email protected]" } ]
I get error :
Error:
Traceback (most recent call last): File "C:\dell\testio_json.py", line 24, in <module> m_data=json.load(m_file) File "C:\dell\pythonwin\lib\json\__init__.py", line 268, in load parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw) File "C:\dell\pythonwin\lib\json\__init__.py", line 319, in loads return _default_decoder.decode(s) File "C:\dell\pythonwin\lib\json\decoder.py", line 339, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\dell\pythonwin\lib\json\decoder.py", line 355, in raw_decode obj, end = self.scan_once(s, idx) json.decoder.JSONDecodeError: Expecting ',' delimiter: line 5 column 2 (char 31)
any idea? thanks
looking at your json file (without testing), it appears that you are missing commas:
Output:
[ { "id":1, "name": "jame", "email": "[email protected]" }, { "id":2, "name": "Apple", "email": "[email protected]" } ]