I have looked for an answer on SO, however no answer was particulary useful. I have a dictionary of values that I have to convert to JSON string. The data looks like this:
In [127]: ddd
Out[127]:
{'ID': {'condition': '',
'data': {1: '2',
2: '3',
3: '4',
4: '5',
5: '6',
6: '7',
7: '8',
8: '9',
9: '10'},
'desc_long': 'Id szko\xc5\x82y',
'desc_short': 'Id szko\xc5\x82y',
'df_name': 'pierszytest',
}}
When I try to execute json.dumps(ddd) I get :
TypeError: keys must be a string
I created a test variable "what"
In [126]: what
Out[126]: {1: '2', 2: '3', 3: '4', 4: '5', 5: '6', 6: '7', 7: '8', 8: '9', 9: '10'}
json.dumps(what) returns:
Out[129]: '{"1": "2", "2": "3", "3": "4", "4": "5", "5": "6", "6": "7", "7": "8", "8": "9", "9": "10"}'
Again, I tried to convert only ddd['ID']['data']:
In [131]: ddd['ID']['data']
Out[131]: {1: '2', 2: '3', 3: '4', 4: '5', 5: '6', 6: '7', 7: '8', 8: '9', 9: '10'}
In [130]: json.dumps(ddd['ID']['data'])
TypeError: keys must be a string
So, this are basically the same variables and still json.dumps fails to deal with the latter. This is a big surpise for me. I have done some research, but it returned nothing useful for my case. If it's a duplicate, please let me know.
* EDIT * I attach result of type() on both variables:
In [132]: type(ddd['ID']['data'])
Out[132]: dict
In [133]: type(what)
Out[133]: dict
type(ddd['ID']['data'])? And can you include the definition of that type in the question, or at least link to it?