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.

Title: json.loads errors out on valid JSON
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.1, Python 3.2, Python 3.3, Python 3.4, Python 3.5, Python 2.7
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Yakov.Keselman, akira, kushal.das
Priority: normal Keywords:

Created on 2014-04-08 23:42 by Yakov.Keselman, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg215780 - (view) Author: Yakov Keselman (Yakov.Keselman) Date: 2014-04-08 23:42
Run the following Python JSON parsing script on valid JSON:

import json
json.loads( '["[\"Residential | Furniture | Cabinets\",\"119.99\"]"]' )


Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python33\lib\json\", line 319, in loads
    return _default_decoder.decode(s)
  File "C:\Python33\lib\json\", line 352, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python33\lib\json\", line 368, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting ',' delimiter: line 1 column 5 (char 4)
msg215803 - (view) Author: Kushal Das (kushal.das) * (Python committer) Date: 2014-04-09 03:33
It is not a valid JSON. You may want to validate it against

What you have inside the string (single quotes) is the JSON representation but not the string representation which json.loads is supposed to parse.
msg215834 - (view) Author: Akira Li (akira) * Date: 2014-04-09 18:37
You need to escape backslashes inside a Python string literal or use raw-string literals:

  >>> import json
  >>> json.loads(r'["[\"Residential | Furniture | Cabinets\",\"119.99\"]"]')
  [u'["Residential | Furniture | Cabinets","119.99"]']

If the backslashes are unintentional then you could remove them:

  >>> json.loads('[["Residential | Furniture | Cabinets","119.99"]]')
  [[u'Residential | Furniture | Cabinets', u'119.99']]

Note: the result is different
Date User Action Args
2022-04-11 14:58:01adminsetgithub: 65381
2014-04-09 19:38:55ned.deilysetstatus: open -> closed
resolution: not a bug
components: + Library (Lib), - IO
stage: resolved
2014-04-09 18:37:40akirasetnosy: + akira
messages: + msg215834
2014-04-09 03:33:11kushal.dassetnosy: + kushal.das
messages: + msg215803
2014-04-08 23:42:13Yakov.Keselmancreate