Closed
Description
Bug report
Bug description:
import json
json.loads('{"a":1,}')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.11/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/json/decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
^^^^^^^^^^^^^^^^^^^^^^
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 8 (char 7)
The error is the trailing comma.
Initial pull proposal: #113047 but apparently this needs more attention than I can contribute.
The actual fix needs to be further down, possibly in line 206
Lines 199 to 208 in 25061f5
we already know that we have seen a comma, and can insert a more helpful error message in line 206.
if nextchar == '}':
raise JSONDecodeError("No trailing commas allowed in JSON objects.", s, end - 1)
at this location, the previous character must have been a comma (line 201). Whitespace has been removed, so this will also catch ,\n}
CPython versions tested on:
3.11
Operating systems tested on:
Linux
Linked PRs
Metadata
Metadata
Assignees
Projects
Status
Done