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 dump with repeated key
Type: Stage: resolved
Components: Versions: Python 3.9, Python 3.8, Python 3.7, Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: facundobatista, rhettinger, veky
Priority: normal Keywords:

Created on 2020-04-02 12:19 by facundobatista, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (6)
msg365581 - (view) Author: Facundo Batista (facundobatista) * (Python committer) Date: 2020-04-02 12:19
As stated in docs [0], JSON structures must not have duplicated keys.

>>> json.dumps({1:2, "1":3})
'{"1": 2, "1": 3}'

This is related to https://bugs.python.org/issue34972 . 



[0] "The RFC specifies that the names within a JSON object should be unique, ..." https://docs.python.org/3/library/json.html#repeated-names-within-an-object
msg365661 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-04-03 01:04
Has this been a problem in practice, or just a theoretical issue?

To make this raise an exception, the JSON encoder would have to add one extra test per key.

I think we should just document the possibility.
msg365939 - (view) Author: Vedran Čačić (veky) * Date: 2020-04-07 20:42
JSON is JavaScript Object Notation, that is, a notation for JS Objects. Python dicts are much more general than that (not only in keys, but in values too: JSON keys must be strings, and values must be Strings, Numbers, Booleans, Arrays, (JS) Objects, or nulls -- both are restricted compared to Python dicts). There is really no reason to expect full embedding.
msg365958 - (view) Author: Facundo Batista (facundobatista) * (Python committer) Date: 2020-04-07 23:07
It's a theoretical issue, I didn't hit it myself.
msg376111 - (view) Author: Facundo Batista (facundobatista) * (Python committer) Date: 2020-08-30 16:27
The balance here is allow an invalid JSON to be created (but documenting that on some situations that will happen), or sticking to always produce valid JSONs, but with a hit in performance (which we don't know so far if it's big or small).
msg376116 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-08-30 16:57
This module has been heavily used in the real world and we have no evidence that there is an actual problem to be solved.  Marking this as closed.  If a real world issue does arise, feel free to reopen and we can consider extending the API to have a validation step.
History
Date User Action Args
2022-04-11 14:59:28adminsetgithub: 84334
2020-08-30 16:57:37rhettingersetstatus: open -> closed
resolution: not a bug
messages: + msg376116

stage: resolved
2020-08-30 16:27:51facundobatistasetmessages: + msg376111
2020-04-07 23:07:10facundobatistasetmessages: + msg365958
2020-04-07 20:42:45vekysetnosy: + veky
messages: + msg365939
2020-04-03 01:04:49rhettingersetnosy: + rhettinger
messages: + msg365661
2020-04-02 12:19:18facundobatistacreate