How to encode bytes in JSON? json.dumps() throwing a TypeError

PythonJsonPython 3.x

Python Problem Overview


I am trying to encode a dictionary containing a string of bytes with json, and getting a is not JSON serializable error:

import base64
import json

data = {}
encoded = base64.b64encode(b'data to be encoded')
data['bytes'] = encoded

print(json.dumps(data))

The error I get:

TypeError: b'ZGF0YSB0byBiZSBlbmNvZGVk\n' is not JSON serializable

How can I correctly encode my dictionary containing bytes with JSON?

Python Solutions


Solution 1 - Python

The JSON format only supports unicode strings. Since base64.b64encode encodes bytes to ASCII-only bytes, you can use that codec to decode the data:

import base64

encoded = base64.b64encode(b'data to be encoded')  # b'ZGF0YSB0byBiZSBlbmNvZGVk' (notice the "b")
data['bytes'] = encoded.decode('ascii')            # 'ZGF0YSB0byBiZSBlbmNvZGVk'

Note that to get the original data back you don't need to re-encode it to bytes because b64decode handles ASCII-only strings as well as bytes:

decoded = base64.b64decode(data['bytes'])  # b'data to be encoded'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFantaView Question on Stackoverflow
Solution 1 - PythonMartijn PietersView Answer on Stackoverflow