Skip to content

Python's json dumps/loads make integer keys of the dict str #76997

Closed

Description

@solin
mannequin
BPO 32816
Nosy @serhiy-storchaka

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-02-10.16:45:44.768>
created_at = <Date 2018-02-10.15:59:52.269>
labels = ['invalid', 'type-bug', 'library']
title = "Python's json dumps/loads make integer keys of the dict str"
updated_at = <Date 2018-02-10.16:45:44.766>
user = 'https://bugs.python.org/solin'

bugs.python.org fields:

activity = <Date 2018-02-10.16:45:44.766>
actor = 'serhiy.storchaka'
assignee = 'none'
closed = True
closed_date = <Date 2018-02-10.16:45:44.768>
closer = 'serhiy.storchaka'
components = ['Library (Lib)']
creation = <Date 2018-02-10.15:59:52.269>
creator = 'solin'
dependencies = []
files = []
hgrepos = []
issue_num = 32816
keywords = []
message_count = 2.0
messages = ['311951', '311953']
nosy_count = 2.0
nosy_names = ['serhiy.storchaka', 'solin']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue32816'
versions = ['Python 3.5']

Activity

  1. solin commented on Feb 10, 2018

    solinmannequin
    MannequinAuthor

    when i serialize and deserialize python built-in structure I'm expect output same as input

    arr2 = [1,2,'3']
    arr2_json = json.dumps(arr2)
    json.loads(arr2_json)
    Out[16]: [1, 2, '3']

    BUT when I'm tring do it with dict I got str keys instead of integer

    dict1 = {0: 'object0', '1': 'object2'}
    json1 = json.dumps(dict1)
    json.loads(json1)
    Out[6]: {'0': 'object0', '1': 'object2'}

    Notice keys must be [0, '1'] but actually are ['0', '1']

  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    type-bugAn unexpected behavior, bug, or error
    on Feb 10, 2018
  3. serhiy-storchaka commented on Feb 10, 2018

    @serhiy-storchaka
    Member

    See the documentation. This is documented behavior. The JSON format supports only string keys, and all keys are coerced to strings when serialized to JSON.

  4. transferred this issue fromon Apr 10, 2022
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

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions