Skip to content

gh-137533: document key type coercion limitations in json.loads #137545

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

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
98ba5d5
gh-137533:Add a note under `function:: loads()` to Improve visibility…
Rov-Waff Aug 8, 2025
dcfd3f3
Merge branch 'main' into fix-issue-137533
Rov-Waff Aug 8, 2025
1161ad0
gh-137533:fix:warp line 396 to 6 lines
Rov-Waff Aug 8, 2025
56dfcfd
gh-137533:add a link for RFC8259
Rov-Waff Aug 8, 2025
40d0cd1
gh-137533:make up more example to explain json.loads
Rov-Waff Aug 9, 2025
9b6732b
gh-137533:make up more example to explain json.loads
Rov-Waff Aug 9, 2025
8c9d81b
gh-137533:just restate the key of json must be string
Rov-Waff Aug 9, 2025
40c5678
gh-137533:restate that keys in JSON must be strings at module-level docs
Rov-Waff Aug 9, 2025
541878a
gh-137533:add RFC link ,then fix a RFC link at line 376
Rov-Waff Aug 9, 2025
e0dc7eb
gh-137533:nuked new note under function:: loads()
Rov-Waff Aug 10, 2025
1814bfa
gh-137533:shorten the sentence of this note,maybe its too short : (
Rov-Waff Aug 10, 2025
f17de69
gh-137533:correct expression
Rov-Waff Aug 10, 2025
a4b8240
gh-137533:move notes under json.dumps() to module-level documents
Rov-Waff Aug 10, 2025
1a71527
gh-137533:fix the note in json.dump()
Rov-Waff Aug 10, 2025
da6e655
gh-137533:make a example in json.loads()
Rov-Waff Aug 10, 2025
3d0b304
gh-137533:nuked notes in json.dumps() and json.loads()
Rov-Waff Aug 10, 2025
d61e84b
gh-137533:make a section about json key convertion,then make some ref
Rov-Waff Aug 11, 2025
96f54a7
gh-137533:remove the notes above part basic usage
Rov-Waff Aug 11, 2025
9bbdb5a
gh-137533:warp too long lines
Rov-Waff Aug 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions Doc/library/json.rst
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ See :ref:`json-commandline` for detailed documentation.
This module's encoders and decoders preserve input and output order by
default. Order is only lost if the underlying containers are unordered.


Basic Usage
-----------

Expand Down Expand Up @@ -256,12 +255,8 @@ Basic Usage

.. note::

Keys in key/value pairs of JSON are always of the type :class:`str`. When
a dictionary is converted into JSON, all the keys of the dictionary are
coerced to strings. As a result of this, if a dictionary is converted
into JSON and then back into a dictionary, the dictionary may not equal
the original one. That is, ``loads(dumps(x)) != x`` if x has non-string
keys.
The encoder dose not preserve the types of dictionary keys in Python.
Read more at :ref:`json-key-convertion`

.. function:: load(fp, *, cls=None, object_hook=None, parse_float=None, \
parse_int=None, parse_constant=None, \
Expand Down Expand Up @@ -364,6 +359,11 @@ Basic Usage
.. versionchanged:: 3.9
The keyword argument *encoding* has been removed.

.. note::

The decoder preserves the keys in JSON text as :class:`str`.
Read more at :ref:`json-key-convertion`


Encoders and Decoders
---------------------
Expand Down Expand Up @@ -577,6 +577,28 @@ Encoders and Decoders
for chunk in json.JSONEncoder().iterencode(bigobject):
mysocket.write(chunk)

.. _json-key-convertion:

JSON Key Convertion
^^^^^^^^^^^^^^^^^^^

:rfc:`7159` requires that keys in key/value pairs of JSON are always of the
type :class:`str`. When a dictionary is converted into JSON, all the keys
of the dictionary arecoerced to strings.When a JSON object is converted into
dictionaries,all the keys of the dictionary are strings.
For example:

>>> import json
>>> foo={1:"spam"}
>>> result=json.dumps(foo)
>>> print(result)
{"1": "spam"}
>>> print(foo)
{1: 'spam'}
>>> print(foo==result)
False

It can be seen that non-string keys are converted into strings after being encoded as JSON

Exceptions
----------
Expand Down
Loading