Linked Questions
15 questions linked to/from Python's 'json' module, converts int dictionary keys to strings
2 votes
2 answers
961 views
save/load dictionary while conserving keys' datatype [duplicate]
I would like to save a dictionary that contains both string and integer keys and multiple datatype values. For example: dData = { 'a': ['c','d'], 1: [5.1, 3.1] } To save and load it I used ...
1 vote
3 answers
1k views
Dict keys change after passing it to json.dump and json.load [duplicate]
I wanted to dump a dictionary into a json file, and later on, load it so then I would use it. dic = {} for n in range(1,10): if n%2==0: dic[n] = n**2 else: dic[str(n)] = n**2 ...
1 vote
1 answer
1k views
JSON dict saving keys as string not int [duplicate]
I have saved a dictionary as json to re-use later across a number of areas. The dictionary structure is: dict = { 1: 'Name', 2: 'Name2', 3: 'Name3' } I have saved it as so: with open('...
-2 votes
3 answers
453 views
Why does this Integer become a string in JavaScript? [duplicate]
So this little snippet of code takes an array A of integers and makes the integers strings when they become the keys of pairs. Why? Python is happy for them to remain integers, but JavaScript seems ...
262 votes
5 answers
279k views
How can I use if/else in a dictionary comprehension?
Does there exist a way in Python 2.7+ to make something like the following? { something_if_true if condition else something_if_false for key, value in dict_.items() } I know you can make ...
125 votes
11 answers
137k views
Using an integer as a key in an associative array in JavaScript
When I create a new JavaScript array, and use an integer as a key, each element of that array up to the integer is created as undefined. For example: var test = new Array(); test[2300] = 'Some string';...
68 votes
4 answers
108k views
Convert a string key to int in a Dictionary
My question is very similar to this one, except I have a dictionary of lists and I'm interested in changing both the key value and all elements in every list form string to int. So for instance I'd ...
54 votes
2 answers
14k views
Python 3, Are there any known security holes in ast.literal_eval(node_or_string)?
Are there any known ways for ast.literal_eval(node_or_string)'s evaluation to not actually be safe? If yes, are patches available for them? (I already know about PyPy[sandbox], which is presumably ...
44 votes
3 answers
33k views
Why do int keys of a python dict turn into strings when using json.dumps?
According to this conversion table, Python ints get written as JSON numbers when serialized using the JSON module--as I would expect and desire. I have a dictionary with an integer key and integer ...
11 votes
2 answers
10k views
Read a JSON and convert the keys to int
It is well known that json converts integer keys of a dict to string: import json print json.dumps({1: [2.5, 2.5, 2.5], 2: [3, 3, 3, 3]}) # {"1": [2.5, 2.5, 2.5], "2": [3, 3, 3, 3]} What's the ...
6 votes
4 answers
4k views
The jsonpickle converts my dictionary keys from numbers to strings
I was trying to serialize my objects in a way that I could read them in the external form and found jsonpickle. But it turns out that it converts my dictionary keys from integers to strings. I ...
0 votes
2 answers
4k views
Python throws KeyError: 1 when using a Dictionary restored from a file
In my program, I have certain settings that can be modified by the user, saved on the disk, and then loaded when application is restarted. Some these settings are stored as dictionaries. While trying ...
3 votes
2 answers
2k views
Pandas to_json turning index into string
The pandas to_json function for some reason is converting the index of the dataframe into strings. Is there a way to avoid this? >>> import pandas as pd >>> df = pd.DataFrame({"a" : ...
2 votes
3 answers
2k views
How to convert Python dict to JSON when some keys are not strings?
The JSON equivalent of a Python dict is a JSON object. However its keys must be strings, that's a well-known limitation. I need to support also boolean and numeric keys. I could make a simple Python ...
0 votes
2 answers
60 views
Why does my JSON create multiple entries instead of updating them?
I have a command where you can enter a correct answer. If it is correct, the user is credited with points in a JSON. But my update function seems to be broken, because after another correct execution ...