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: Error msg from using wrong quotes in JSON is unhelpful
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: bob.ippolito, cben, ezio.melotti, georg.brandl, ggenellina, pitrou, python-dev, rhettinger, serhiy.storchaka, steven.daprano
Priority: normal Keywords: patch

Created on 2009-01-26 06:24 by steven.daprano, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
json-messages.diff ggenellina, 2009-01-27 01:06
json-messages-2.patch serhiy.storchaka, 2012-06-15 09:30 review
Messages (12)
msg80564 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2009-01-26 06:24
Using the wrong sort of quotes in json gives unhelpful error messages:

>>> json.loads("{'test':'test'}")
Traceback (most recent call last):
  ...
ValueError: Expecting property name: line 1 column 1 (char 1)

Unless you know that strings in JSON must be delimited with 
double-quotes and not single (a very surprising fact to those used to 
Python) this error message is perplexing. I suggest something like:

Single-quoted strings are invalid property names: line 1 column 1 
(char 1)

or 

Parse error, invalid char: line 1, column 1 (char 1)
msg80604 - (view) Author: Gabriel Genellina (ggenellina) Date: 2009-01-27 01:05
This patch provides a better error message for this case::

  json.loads("""{'test': "test"}""")

but still doesn't help in this one::

  json.loads("""{"test": 'test'}""")

'test' looks like garbage to JSON (it *is* garbage!), exactly the same 
as::

  json.loads("""{"test": @?&%%}""")

so it's hard to provide a better message when the parser expects a 
generic object.
msg82949 - (view) Author: Cherniavsky Beni (cben) * Date: 2009-03-01 00:52
Perhaps it should not be an error at all?  The default should probably
stay strict to the spec, but IMHO the module should provide an optional
lenient parsing mode that also accepts single quotes.

Why support single quotes and not any other imaginable deviation from
the spec?  Because single quotes are the only way (AFAIK) in which
Python's repr() produces invalid JSON (from JSONable combinations of types).
msg82951 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-03-01 01:48
+1 on Steven's request for a better error message.

+1 on Beni's request for looser input requirements for better
interoperability with Python's repr.  OTOH, I've never found it hard to
write:  s.replace("'", '"').
msg82970 - (view) Author: Bob Ippolito (bob.ippolito) * (Python committer) Date: 2009-03-01 18:00
I don't really want to see looser input requirements, making a JSON 
parser that is compatible with a subset of Python repr output isn't a 
design goal of mine.

This is absolutely false:
"Because single quotes are the only way (AFAIK) in which
Python's repr() produces invalid JSON (from JSONable combinations of 
types)."

>>> repr(object)
"<type 'object'>"

If you don't know JSON, I'm not sure throwing random input at the JSON 
parser is going to help you. Is that how you learned XML? There's plenty 
of info in the JSON documentation and a link to json.org if you need 
help.
msg82971 - (view) Author: Bob Ippolito (bob.ippolito) * (Python committer) Date: 2009-03-01 18:04
Er, sorry, missed "(from JSONable combinations of types)". It's early.

Anyway, I can change the error message, but I will not make it special-
case single quotes for its own error message. I will have to think about 
what the message could say instead. Note that the same sort of person who 
throws random input at parsers might even expect {test: 'test'} to work 
since that is valid JavaScript but not valid JSON.
msg162878 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-06-15 09:30
Patch adapted for Python 3.3. Consistently changed messages in C code, docs and docstrings.
msg163581 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-06-23 11:25
Any chance to commit the patch today and to get this feature in Python 3.3?
msg163841 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-06-24 20:16
I would say this is a bugfix, so it can go in after the beta. Georg?
msg163845 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2012-06-24 20:21
Agreed.
msg164304 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-06-29 00:03
New changeset 9854520c8200 by Antoine Pitrou in branch '3.2':
Issue #5067: improve some json error messages.
http://hg.python.org/cpython/rev/9854520c8200

New changeset 7523ab4e6e06 by Antoine Pitrou in branch 'default':
Issue #5067: improve some json error messages.
http://hg.python.org/cpython/rev/7523ab4e6e06

New changeset 7762816e3fcd by Antoine Pitrou in branch '2.7':
Issue #5067: improve some json error messages.
http://hg.python.org/cpython/rev/7762816e3fcd
msg164305 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-06-29 00:04
Committed now. Thanks Serhiy for the patch!
History
Date User Action Args
2022-04-11 14:56:44adminsetgithub: 49317
2012-06-29 00:04:19pitrousetstatus: open -> closed
resolution: fixed
messages: + msg164305

stage: patch review -> resolved
2012-06-29 00:03:51python-devsetnosy: + python-dev
messages: + msg164304
2012-06-24 20:24:56ezio.melottisetnosy: + ezio.melotti
2012-06-24 20:21:10georg.brandlsetmessages: + msg163845
2012-06-24 20:16:25pitrousetnosy: + pitrou, georg.brandl
messages: + msg163841

assignee: bob.ippolito ->
components: - Documentation
stage: patch review
2012-06-23 11:25:57serhiy.storchakasetmessages: + msg163581
2012-06-15 09:30:47serhiy.storchakasetfiles: + json-messages-2.patch
versions: + Python 2.7, Python 3.2, Python 3.3, - Python 2.6
nosy: + serhiy.storchaka

messages: + msg162878

components: + Documentation
2009-03-01 18:04:00bob.ippolitosetmessages: + msg82971
2009-03-01 18:00:56bob.ippolitosetmessages: + msg82970
2009-03-01 01:48:17rhettingersetassignee: bob.ippolito
messages: + msg82951
nosy: + bob.ippolito, rhettinger
2009-03-01 00:52:46cbensetnosy: + cben
messages: + msg82949
2009-01-27 01:06:13ggenellinasetfiles: + json-messages.diff
keywords: + patch
2009-01-27 01:05:49ggenellinasetnosy: + ggenellina
messages: + msg80604
2009-01-26 12:45:00steven.dapranosettype: behavior
components: + Library (Lib)
2009-01-26 06:24:32steven.dapranocreate