json.dumps on dictionary with bytes for keys
you could pre-process the dictionary to convert the keys as strings recursively if they're bytes
import json
# your dump code for values, unmodified
class BytesDump(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, bytes):
return obj.decode()
return json.JSONEncoder.default(self, obj)
# recursive key as string conversion for byte keys
def keys_string(d):
rval = {}
if not isinstance(d, dict):
if isinstance(d,(tuple,list,set)):
v = [keys_string(x) for x in d]
return v
else:
return d
for k,v in d.items():
if isinstance(k,bytes):
k = k.decode()
if isinstance(v,dict):
v = keys_string(v)
elif isinstance(v,(tuple,list,set)):
v = [keys_string(x) for x in v]
rval[k] = v
return rval
print(json.dumps(keys_string(bar), cls=BytesDump))
with:
bar = {b'name': b'bob', b'age': 33, b'attributes': {b'hair': b'brown', b'arms': 2},
b'other': [{b'hair': b'brown', b'arms': 2}]}
prints:
{"attributes": {"hair": "brown", "arms": 2}, "age": 33, "name": "bob", "other": [{"hair": "brown", "arms": 2}]}
Looks like you need to use a recursive utility function:
import json
def decode_dict(d):
result = {}
for key, value in d.items():
if isinstance(key, bytes):
key = key.decode()
if isinstance(value, bytes):
value = value.decode()
elif isinstance(value, dict):
value = decode_dict(value)
result.update({key: value})
return result
bar = {b'name': b'bob', b'age': 33, b'attributes': {b'hair': b'brown', b'arms': 2}}
print(json.dumps(decode_dict(bar)))
Output:
{"name": "bob", "age": 33, "attributes": {"hair": "brown", "arms": 2}}