This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Improve error messages for malformed JSON
Type: enhancement Stage: resolved
Components: Versions: Python 3.5
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Json error messages could provide more information about the error
View: 16009
Assigned To: Nosy List: r.david.murray, rhettinger
Priority: normal Keywords:

Created on 2014-05-07 16:51 by rhettinger, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg218062 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-05-07 16:51
The error message for malformed JSON just tells you that the JSON is invalid, it doesn't say why (showing you which character bombed, what text is being read, what the pending openers are, or what allowable characters would have been expected).  In the absence of this information, it is very difficult to debug hand-rolled JSON.

   json.loads('sample_file.json')     # students find this hard to debug

   json.loads('''[
          "boys": 10,
          "girls": 20,
         ]''')                        # hard to see trailing comma

   json.loads("['python', 'perl', 'ruby']")  # needs double quotes

   json.loads("[[10, 20], [30, 40]]]") # unbalanced delimiters
msg218065 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-05-07 17:55
Python 3.4.0+ (3.4:d994d75cce95, May  6 2014, 21:37:02) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> json.loads('''[
...           "boys": 10,
...           "girls": 20,
...          ]''')
Traceback (most recent call last):
   ...
ValueError: Expecting ',' delimiter: line 2 column 17 (char 18)
>>> json.loads("['python', 'perl', 'ruby']")
Traceback (most recent call last):
   ...
ValueError: Expecting value: line 1 column 2 (char 1)
>>> json.loads("[[10, 20], [30, 40]]]")
Traceback (most recent call last):
   ...
ValueError: Extra data: line 1 column 21 - line 1 column 22 (char 20 - 21)

See issue 16009 for when this was added. Can you improve these? (The first one looks odd...)  If so, you could reopen this.
msg218066 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-05-07 18:00
What do you think about backporting the improved error messages?
msg218069 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-05-07 18:28
Hmm.  I guess I don't have any objection in principle.  It is hard to imagine why someone would depend on the exact format of the old low-information messages, though anything is possible.
History
Date User Action Args
2022-04-11 14:58:03adminsetgithub: 65650
2014-05-07 18:28:08r.david.murraysetmessages: + msg218069
2014-05-07 18:00:39rhettingersetmessages: + msg218066
2014-05-07 17:55:44r.david.murraysetstatus: open -> closed

superseder: Json error messages could provide more information about the error
versions: - Python 2.7, Python 3.4
nosy: + r.david.murray

messages: + msg218065
resolution: duplicate
stage: resolved
2014-05-07 16:51:27rhettingercreate