Skip to content
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

json.loads errors out on valid JSON #65381

Closed
YakovKeselman mannequin opened this issue Apr 8, 2014 · 3 comments
Closed

json.loads errors out on valid JSON #65381

YakovKeselman mannequin opened this issue Apr 8, 2014 · 3 comments
Labels
stdlib Python modules in the Lib dir

Comments

@YakovKeselman
Copy link
Mannequin

YakovKeselman mannequin commented Apr 8, 2014

BPO 21182
Nosy @4kir4, @kushaldas

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2014-04-09.19:38:55.181>
created_at = <Date 2014-04-08.23:42:13.544>
labels = ['invalid', 'library']
title = 'json.loads errors out on valid JSON'
updated_at = <Date 2014-04-09.19:38:55.179>
user = 'https://bugs.python.org/YakovKeselman'

bugs.python.org fields:

activity = <Date 2014-04-09.19:38:55.179>
actor = 'ned.deily'
assignee = 'none'
closed = True
closed_date = <Date 2014-04-09.19:38:55.181>
closer = 'ned.deily'
components = ['Library (Lib)']
creation = <Date 2014-04-08.23:42:13.544>
creator = 'Yakov.Keselman'
dependencies = []
files = []
hgrepos = []
issue_num = 21182
keywords = []
message_count = 3.0
messages = ['215780', '215803', '215834']
nosy_count = 3.0
nosy_names = ['akira', 'kushal.das', 'Yakov.Keselman']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue21182'
versions = ['Python 3.1', 'Python 2.7', 'Python 3.2', 'Python 3.3', 'Python 3.4', 'Python 3.5']

@YakovKeselman
Copy link
Mannequin Author

YakovKeselman mannequin commented Apr 8, 2014

Run the following Python JSON parsing script on valid JSON:

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

Result:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python33\lib\json\__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "C:\Python33\lib\json\decoder.py", line 352, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python33\lib\json\decoder.py", line 368, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting ',' delimiter: line 1 column 5 (char 4)

@YakovKeselman YakovKeselman mannequin added the topic-IO label Apr 8, 2014
@kushaldas
Copy link
Member

It is not a valid JSON. You may want to validate it against http://jsonlint.com/

What you have inside the string (single quotes) is the JSON representation but not the string representation which json.loads is supposed to parse.

@4kir4
Copy link
Mannequin

4kir4 mannequin commented Apr 9, 2014

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

@ned-deily ned-deily added stdlib Python modules in the Lib dir and removed topic-IO labels Apr 9, 2014
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir
Projects
None yet
Development

No branches or pull requests

2 participants