Json Dumping A Dict Throws Typeerror: Keys Must Be A String
Solution 1:
You could try to clean it up like this:
for key in mydict.keys():
iftype(key) isnotstr:
try:
mydict[str(key)] = mydict[key]
except:
try:
mydict[repr(key)] = mydict[key]
except:
passdel mydict[key]
This will try to convert any key that is not a string into a string. Any key that could not be converted into a string or represented as a string will be deleted.
Solution 2:
Modifying the accepted answer above, I wrote a function to handle dictionaries of arbitrary depth:
defstringify_keys(d):
"""Convert a dict's keys to strings if they are not."""for key in d.keys():
# check inner dictifisinstance(d[key], dict):
value = stringify_keys(d[key])
else:
value = d[key]
# convert nonstring to string if neededifnotisinstance(key, str):
try:
d[str(key)] = value
except Exception:
try:
d[repr(key)] = value
except Exception:
raise# delete old keydel d[key]
return d
Solution 3:
I know this is an old question and it already has an accepted answer, but alas the accepted answer is just totally wrong.
The real issue here is that the code that generates the dict uses the builtin id
function as key instead of the literal string "id"
. So the simple, obvious and only correct solution is to fix this bug at the source : check the code that generates the dict, and replace id
with "id"
.
Solution 4:
Maybe this will help:
your_dict = {("a", "b"):[1,2,3,4]}
# savewithopen("file.json","w") as f:
f.write(json.dumps(list(your_dict.items())))
# loadwithopen("file.json","r") as f:
your_dict = dict([tuple((tuple(x[0]), x[1])) for x in json.loads(f.read())])
Solution 5:
Nolan conaway's answer gives this result for example
{"b'opening_hours'": {"b'1_from_hour'": 720, "b'1_to_hour'": 1440, "b'1_break_from_hour'": 1440, "b'1_break_to_hour'": 1440, "b'2_from_hour'": 720, "b'2_to_hour'": 1440, "b'2_break_from_hour'": 1440, "b'2_break_to_hour'": 1440, "b'3_from_hour'": 720, "b'3_to_hour'": 1440, "b'3_break_from_hour'": 1440, "b'3_break_to_hour'": 1440, "b'4_from_hour'": 720, "b'4_to_hour'": 1440, "b'4_break_from_hour'": 1440, "b'4_break_to_hour'": 1440, "b'5_from_hour'": 720, "b'5_to_hour'": 1440, "b'5_break_from_hour'": 1440, "b'5_break_to_hour'": 1440, "b'6_from_hour'": 720, "b'6_to_hour'": 1440, "b'6_break_from_hour'": 1440, "b'6_break_to_hour'": 1440, "b'7_from_hour'": 720, "b'7_to_hour'": 1440, "b'7_break_from_hour'": 1440, "b'7_break_to_hour'": 1440}}
while this amended version
import time
import re
import json
from phpserialize import *
classHelpers:
defstringify_keys(self,d):
"""Convert a dict's keys to strings if they are not."""for key in d.keys():
# check inner dictifisinstance(d[key], dict):
value = Helpers().stringify_keys(d[key])
else:
value = d[key]
# convert nonstring to string if neededifnotisinstance(key, str):
try:
d[key.decode("utf-8")] = value
except Exception:
try:
d[repr(key)] = value
except Exception:
raise# delete old keydel d[key]
return d
will give this cleaner version..
{"opening_hours": {"1_from_hour": 720, "1_to_hour": 1440, "1_break_from_hour": 1440, "1_break_to_hour": 1440, "2_from_hour": 720, "2_to_hour": 1440, "2_break_from_hour": 1440, "2_break_to_hour": 1440, "3_from_hour": 720, "3_to_hour": 1440, "3_break_from_hour": 1440, "3_break_to_hour": 1440, "4_from_hour": 720, "4_to_hour": 1440, "4_break_from_hour": 1440, "4_break_to_hour": 1440, "5_from_hour": 720, "5_to_hour": 1440, "5_break_from_hour": 1440, "5_break_to_hour": 1440, "6_from_hour": 720, "6_to_hour": 1440, "6_break_from_hour": 1440, "6_break_to_hour": 1440, "7_from_hour": 720, "7_to_hour": 1440, "7_break_from_hour": 1440, "7_break_to_hour": 1440}}
Post a Comment for "Json Dumping A Dict Throws Typeerror: Keys Must Be A String"