Displaying better error message than "No JSON object could be decoded" Ask Question

Displaying better error message than

Python code to load data from some long complicated JSON file:

with open(filename, "r") as f:
  data = json.loads(f.read())

(note: the best code version should be:

with open(filename, "r") as f:
  data = json.load(f)

but both exhibit similar behavior)

For many types of JSON error (missing delimiters, incorrect backslashes in strings, etc), this prints a nice helpful message containing the line and column number where the JSON error was found.

However, for other types of JSON error (including the classic "using comma on the last item in a list", but also other things like capitalising true/false), Python's output is just:

Traceback (most recent call last):
  File "myfile.py", line 8, in myfunction
    config = json.loads(f.read())
  File "c:\python27\lib\json\__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "c:\python27\lib\json\decoder.py", line 360, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "c:\python27\lib\json\decoder.py", line 378, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

For that type of ValueError, how do you get Python to tell you where is the error in the JSON file?

ベストアンサー1

simplejson組み込みモジュールが曖昧な場合、多くの場合、モジュールはより詳細なエラーを返すことがわかりましたjson。たとえば、リストの最後の項目の後にカンマがある場合:

json.loads('[1,2,]')
....
ValueError: No JSON object could be decoded

これはあまり説明的ではありません。同じ操作を次のように行いますsimplejson

simplejson.loads('[1,2,]')
...
simplejson.decoder.JSONDecodeError: Expecting object: line 1 column 5 (char 5)

ずっと良くなりました! 大文字にするなどの他の一般的なエラーについても同様ですTrue

おすすめ記事