1

I tried to read data from a JSON file, but I encountered weird error and have no idea what it means. I tried googling it, but it didn't help. I got the following error:

Traceback (most recent call last):
 File "items_uploader.py", line 40, in <module>
 main()
 File "items_uploader.py", line 16, in main
 LoadItemsData(settings['items_filename'])
 File "items_uploader.py", line 36, in LoadItemsData
 data = json.load(json_data)
 File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 278, in load
 **kw)
 File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 326, in loads
 return _default_decoder.decode(s)
 File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 366, in decode
 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
 File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 382, in raw_decode
 obj, end = self.scan_once(s, idx)
ValueError: Expecting , delimiter: line 8 column 397 (char 3064)

The code itself is quite simple:

import socket
import MySQLdb
from ConfigParser import SafeConfigParser
import json
from pprint import pprint
def main():
 settings = GetSettings()
 LoadItemsData(settings['items_filename'])
 return
def GetSettings():
 settings = {}
 parser = SafeConfigParser()
 parser.read('settings.yaml')
 settings['items_filename'] = parser.get('files', 'items_filename')
 return settings
def LoadItemsData(filename):
 json_data=open(filename)
 data = json.load(json_data)
 return data
if __name__ == '__main__':
 main()

Any help would be appreciated!

Nathan
4,3974 gold badges22 silver badges21 bronze badges
asked Dec 3, 2012 at 0:01
6
  • 3
    Are you sure your JSON data is valid? The error appears to be a syntax error in your input data. Commented Dec 3, 2012 at 0:05
  • 2
    Please include the portion of the file around where the JSON error occurs (line 8 column 397, char 3064). Commented Dec 3, 2012 at 0:05
  • Basically none of the python code you posted (other than the traceback) is relevant here, as the only important thing is the contents of settings['items_filename'] file. Commented Dec 3, 2012 at 0:14
  • Could you show us your JSON data? Commented Dec 3, 2012 at 0:33
  • Completely unrelated, but using CamelCase in function names in Python is frowned upon, this style is reserved for classes. python.org/dev/peps/pep-0008/#naming-conventions Commented Dec 3, 2012 at 0:48

1 Answer 1

3

Make sure your JSON data is in a valid format, one extra character will mess up the python parser. To test your JSON data go here, make sure you can see it in a correct format.

For example, if I had

JSON_data ='{"c":[{"xy":{"xstart":0,"xend":5,"ystart":1,"yend":5},"names":["D","T","O","H","L","C",],"co":["rgb(0,0,128)"]}],"Values":{"D":["11/30/2012"],"T":["09:44:00"],"O":["5848.40"],"H":["5848.40"],"L":["5847.45"],"C":["5848.40"]}}'

The , after C (here ["D","T","O","H","L","C",]) will show an error. So make sure that your data is in correct format and there are no unnecessary characters.

Hope this helps.

answered Dec 3, 2012 at 3:01
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it turns out it was a bug in JSON file!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.