Skip to content

json dump silently converts int keys to string #79153

Closed

Description

@My-TienNguyen
BPO 34972
Nosy @facundobatista, @ericvsmith, @zooba, @tirkarthi

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 2018-10-14.11:33:13.954>
created_at = <Date 2018-10-13.10:16:46.343>
labels = ['invalid', 'type-bug', 'library']
title = 'json dump silently converts int keys to string'
updated_at = <Date 2020-04-06.06:14:52.103>
user = 'https://bugs.python.org/My-TienNguyen'

bugs.python.org fields:

activity = <Date 2020-04-06.06:14:52.103>
actor = 'stub'
assignee = 'none'
closed = True
closed_date = <Date 2018-10-14.11:33:13.954>
closer = 'My-Tien Nguyen'
components = ['Library (Lib)']
creation = <Date 2018-10-13.10:16:46.343>
creator = 'My-Tien Nguyen'
dependencies = []
files = []
hgrepos = []
issue_num = 34972
keywords = []
message_count = 9.0
messages = ['327649', '327650', '327654', '327684', '327688', '327703', '361302', '365838', '365840']
nosy_count = 7.0
nosy_names = ['facundobatista', 'eric.smith', 'stub', 'steve.dower', 'xtreak', 'My-Tien Nguyen', 'Stub2']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue34972'
versions = ['Python 3.6']

Activity

  1. My-TienNguyen commented on Oct 13, 2018

    My-TienNguyenmannequin
    MannequinAuthor

    When int keys are silently converted to string on json serialization, the user needs to remember to convert it back to int on loading.
    I think that a warning should be shown at least.

    In my case I serialize a dict to json with int keys, later load it back into a dict (resulting in a dict with string keys) and test for existence of an int key in the dict which will then return False incorrectly.

    I am aware that json does not support int keys, but this can be easily forgotten.

  2. added
    stdlib
    type-bug
    on Oct 13, 2018
  3. tirkarthi commented on Oct 13, 2018

    @tirkarthi
    Member

    Thanks for the report. There was a related issue few days back bpo-32816. I think this is a documented behavior at https://docs.python.org/3.8/library/json.html#json.dumps . Having a warning in place might break code and I don't know if there is a safe way to introduce this as a code level warning given that this is a documented behavior in Python 2 and 3. I think this is the case with other languages too like JavaScript itself converting int to string without warning adhering to JSON standard. Correct me if I am wrong or other languages have a warning related to this

    Note: Keys in key/value pairs of JSON are always of the type 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

    You can try doing json.loads(data, parse_int=int) but it will try converting the values.

    >>> json.loads(json.dumps({1:'1'}), parse_int=int)
    {'1': '1'}
    >>> json.loads(json.dumps({1:1}), parse_int=int) 
    {'1': 1}

    Thanks

  4. My-TienNguyen commented on Oct 13, 2018

    My-TienNguyenmannequin
    MannequinAuthor

    I don’t think, “other languages do that too” is a good argument here. This would apply if behaving differently would break user expectation. But here we would do nothing more than explicitly inform the user of a relevant operation. If they already expected that behaviour, they can disregard the warning.

    I don’t see how parse_intwould help me here, I would need a parse_str=int, but then it would try to parse every string, and I don’t see the use case for that.

    I would suggest a warning similar to this:

    --- json/encoder.py
    +++ json/encoder.py
    @@ -1,6 +1,7 @@
     """Implementation of JSONEncoder
     """
     import re
    +import warnings
     
     try:
         from _json import encode_basestring_ascii as c_encode_basestring_ascii
    @@ -353,7 +354,9 @@
                 items = sorted(dct.items(), key=lambda kv: kv[0])
             else:
                 items = dct.items()
    +        non_str_key = False
             for key, value in items:
    +            non_str_key = non_str_key or not isinstance(key, str)
                 if isinstance(key, str):
                     pass
                 # JavaScript is weakly typed for these, so it makes sense to
    @@ -403,6 +406,8 @@
                     else:
                         chunks = _iterencode(value, _current_indent_level)
                     yield from chunks
    +        if non_str_key:
    +            warnings.warn("Encountered non-string key(s), converted to string.", RuntimeWarning)
             if newline_indent is not None:
                 _current_indent_level -= 1
                 yield '\n' + _indent * _current_indent_level
  5. ericvsmith commented on Oct 14, 2018

    @ericvsmith
    Member

    I can't think of another place where we issue a warning for anything similar. I'm opposed to any changes here: it's clearly documented behavior.

    It's like being surprised .ini files convert to strings: it's just how that format works.

  6. zooba commented on Oct 14, 2018

    @zooba
    Member

    Agreed with Eric. json.dump needs to produce valid JSON, which requires keys to be strings.

    Try using pickle if you need to preserve full Python semantics.

  7. My-TienNguyen commented on Oct 14, 2018

    My-TienNguyenmannequin
    MannequinAuthor

    Sure, I can do that, but wanted to propose this regardless. I guess this is a disagreement on a language design level.
    As a proponent of strong typing I wouldn’t have allowed non-string keys in the first place, and if they are allowed I would warn about conversion. This is also more aligned with the “explicit is better than implicit” principle.

  8. facundobatista commented on Feb 3, 2020

    @facundobatista
    Member

    I understand (and agree with) the merits of automatically converting the int to str when dumping to a string.

    However, this result really surprised me:

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

    Is it a valid JSON?

  9. Stub2 commented on Apr 6, 2020

    Stub2mannequin
    Mannequin

    Similarly, keys can be lost entirely:

    >>> json.dumps({1:2, 1.0:3})
    '{"1": 3}'
  10. stub commented on Apr 6, 2020

    stubmannequin
    Mannequin

    (sorry, my example is normal Python behavior. {1:1, 1.0:2} == {1:2} , {1.0:1} == {1:1} )

  11. transferred this issue fromon Apr 10, 2022
  12. GJoe2 commented on Aug 7, 2025

    @GJoe2

    I had this issue recently and spend some time . While I understand json format requires keys to be strings and its specified in RFC 7159 that doesn't mean every user should know about that, I suggest writing a warning in the documentation front page would save time for everyone

  13. facundobatista commented on Aug 7, 2025

    @facundobatista
    Member

    @GJoe2 +1, I think a paragraph there is worth it. May you suggest some wording or a draft for it? Thanks!

    We should probably have a new ticket for this doc change; if you open one, please assign it to me so I can take care. Thanks again!

  14. ericvsmith commented on Aug 7, 2025

    @ericvsmith
    Member

    I agree that a warning in the documentation would be helpful.

  15. GJoe2 commented on Aug 7, 2025

    @GJoe2

    @facundobatista I open a new issue at #137533, check it out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibtype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions