Skip to content

Improving error message with trailing comma in json #113149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kno10 opened this issue Dec 14, 2023 · 1 comment · Fixed by #113227
Closed

Improving error message with trailing comma in json #113149

kno10 opened this issue Dec 14, 2023 · 1 comment · Fixed by #113227
Labels
3.13 bugs and security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@kno10
Copy link

kno10 commented Dec 14, 2023

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

cpython/Lib/json/decoder.py

Lines 199 to 208 in 25061f5

if nextchar == '}':
break
elif nextchar != ',':
raise JSONDecodeError("Expecting ',' delimiter", s, end - 1)
end = _w(s, end).end()
nextchar = s[end:end + 1]
end += 1
if nextchar != '"':
raise JSONDecodeError(
"Expecting property name enclosed in double quotes", s, end - 1)

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

@kno10 kno10 added the type-bug An unexpected behavior, bug, or error label Dec 14, 2023
@sunmy2019 sunmy2019 added type-feature A feature request or enhancement stdlib Python modules in the Lib dir labels Dec 15, 2023
@sunmy2019 sunmy2019 changed the title Misleading error message with trailing comma in json Improving error message with trailing comma in json Dec 15, 2023
@sunmy2019 sunmy2019 removed the type-bug An unexpected behavior, bug, or error label Dec 15, 2023
@serhiy-storchaka
Copy link
Member

You are welcome. Looks like it will work. Do you want to create a PR? A similar special case for a trailing comma can be added for parsing JSON array.

@serhiy-storchaka serhiy-storchaka added the 3.13 bugs and security fixes label Dec 16, 2023
cjwatson added a commit to cjwatson/ionit that referenced this issue Jan 12, 2025
The fix for python/cpython#113149 changed this
error message.

Closes: #1092519
bdrung pushed a commit to bdrung/ionit that referenced this issue Jan 13, 2025
The fix for python/cpython#113149 changed this
error message.

Closes: #1092519
bdrung pushed a commit to bdrung/ionit that referenced this issue Jan 13, 2025
The fix for python/cpython#113149 changed this
error message.

Closes: #1092519
cjwatson added a commit to cjwatson/ionit that referenced this issue Jan 13, 2025
The fix for python/cpython#113149 changed this
error message.

Closes: #1092519
bdrung pushed a commit to bdrung/ionit that referenced this issue Jan 13, 2025
The fix for python/cpython#113149 changed this
error message.

Closes: #1092519
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants