Skip to content
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

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

Closed
solin mannequin opened this issue Feb 10, 2018 · 2 comments
Closed

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

solin mannequin opened this issue Feb 10, 2018 · 2 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@solin
Copy link
Mannequin

solin mannequin commented Feb 10, 2018

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']

@solin
Copy link
Mannequin Author

solin mannequin commented Feb 10, 2018

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']

@solin solin mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Feb 10, 2018
@serhiy-storchaka
Copy link
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.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant