Skip to content
Advertisement

json.dumps on dictionary with bytes for keys

Trying to convert dictionary object with keys of type bytes to json using json.dumps(). Format of dictionary object is not known beforehand. Have found solution for arrays or dictionaries with byte values when using json.dumps (Convert bytes embedded in list (or dict) to str for use with json.dumps) but have not found one for byte keys.

JavaScript

Output from above

JavaScript

Advertisement

Answer

you could pre-process the dictionary to convert the keys as strings recursively if they’re bytes

JavaScript

with:

JavaScript

prints:

JavaScript
Advertisement