Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Improving error message with trailing comma in json #113149

Closed
Labels
3.13bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement
@kno10

Description

@kno10

Bug report

Bug description:

importjsonjson.loads('{"a":1,}')Traceback (mostrecentcalllast):File"<stdin>",line1,in<module>File"/usr/lib/python3.11/json/__init__.py",line346,inloadsreturn_default_decoder.decode(s)^^^^^^^^^^^^^^^^^^^^^^^^^^File"/usr/lib/python3.11/json/decoder.py",line337,indecodeobj,end=self.raw_decode(s,idx=_w(s,0).end())^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File"/usr/lib/python3.11/json/decoder.py",line353,inraw_decodeobj,end=self.scan_once(s,idx)^^^^^^^^^^^^^^^^^^^^^^json.decoder.JSONDecodeError:Expectingpropertynameenclosedindoublequotes:line1column8 (char7)

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

ifnextchar=='}':
break
elifnextchar!=',':
raiseJSONDecodeError("Expecting ',' delimiter",s,end-1)
end=_w(s,end).end()
nextchar=s[end:end+1]
end+=1
ifnextchar!='"':
raiseJSONDecodeError(
"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.

ifnextchar=='}':raiseJSONDecodeError("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

No one assigned

    Labels

    3.13bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp