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: json.dumps serializes double quotes incorrectly
Type: behavior Stage: resolved
Components: Versions: Python 3.6, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Juozas.Masiulis, serhiy.storchaka
Priority: normal Keywords:

Created on 2018-10-08 14:11 by Juozas.Masiulis, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg327352 - (view) Author: Juozas Masiulis (Juozas.Masiulis) Date: 2018-10-08 14:11
currently python behaves like this:
>>> import json
>>> a={'a': '//a[@asdf="asdf"]'}
>>> json.dumps(a)
'{"a": "//a[@asdf=\\"asdf\\"]"}'

this behaviour is incorrect.

the resulting string should be '{"a": "//a[@asdf=\"asdf\"]"}'

The difference is that double quotes inside double quotes are escaped twice instead of once. 

compare it to behaviour in javascript:

> var a = {'a': '//a[@asdf="asdf"]'}
undefined

JSON.stringify(a)
"{"a":"//a[@asdf=\"asdf\"]"}"
msg327353 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-10-08 14:25
>>> print(json.dumps(a))
{"a": "//a[@asdf=\"asdf\"]"}

You seen the repr or the resulting string, not the resulting string itself.
History
Date User Action Args
2022-04-11 14:59:06adminsetgithub: 79114
2018-10-08 14:25:22serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg327353

resolution: not a bug
stage: resolved
2018-10-08 14:11:26Juozas.Masiuliscreate