Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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!

Are you sure your JSON data is valid? The error appears to be a syntax error in your input data. – Jesse the Game Dec 3, 2012 at 0:05 Please include the portion of the file around where the JSON error occurs (line 8 column 397, char 3064). – Andrew Clark 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. – jdi Dec 3, 2012 at 0:14 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 – Sergey Dec 3, 2012 at 0:48

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.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.