From 0a4061af6485ad3ed4759ec189a29bc9d5ba9df0 Mon Sep 17 00:00:00 2001 From: Erich Schubert Date: Wed, 13 Dec 2023 10:33:12 +0100 Subject: [PATCH 1/2] More helpful error message in JSON decoder While JavaScript typically parses the following example, it is not strictly valid JSON. But while the current Python error message `Expecting property name enclosed in double quotes` hints at a missing quote, the problem in fact is a trailing comma: ```js { "key": "value", } ``` --- Lib/json/decoder.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/json/decoder.py b/Lib/json/decoder.py index c5d9ae2d0d5d04..bb1fe9ed273f2c 100644 --- a/Lib/json/decoder.py +++ b/Lib/json/decoder.py @@ -161,7 +161,7 @@ def JSONObject(s_and_end, strict, scan_once, object_hook, object_pairs_hook, return pairs, end + 1 elif nextchar != '"': raise JSONDecodeError( - "Expecting property name enclosed in double quotes", s, end) + "Expecting a property name enclosed in double quotes (no trailing comma allowed)", s, end) end += 1 while True: key, end = scanstring(s, end, strict) From 5a99894f4a7321955a4e10e56241f241e9f6e2ed Mon Sep 17 00:00:00 2001 From: Erich Schubert Date: Thu, 14 Dec 2023 22:58:27 +0100 Subject: [PATCH 2/2] Better fix, untested. --- Lib/json/decoder.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/json/decoder.py b/Lib/json/decoder.py index bb1fe9ed273f2c..17caf7001b41a5 100644 --- a/Lib/json/decoder.py +++ b/Lib/json/decoder.py @@ -161,7 +161,7 @@ def JSONObject(s_and_end, strict, scan_once, object_hook, object_pairs_hook, return pairs, end + 1 elif nextchar != '"': raise JSONDecodeError( - "Expecting a property name enclosed in double quotes (no trailing comma allowed)", s, end) + "Expecting property name enclosed in double quotes", s, end) end += 1 while True: key, end = scanstring(s, end, strict) @@ -203,6 +203,8 @@ def JSONObject(s_and_end, strict, scan_once, object_hook, object_pairs_hook, end = _w(s, end).end() nextchar = s[end:end + 1] end += 1 + if nextchar == '}': + raise JSONDecodeError("Trailing commas are not allowed in JSON objects.") if nextchar != '"': raise JSONDecodeError( "Expecting property name enclosed in double quotes", s, end - 1)