How to encode bytes in JSON? json.dumps() throwing a TypeError
<p>I am trying to encode a dictionary containing a string of bytes with <code>json</code>, and getting a <code>is not JSON serializable error</code>:</p>
<pre><code>import base64
import json

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

print(json.dumps(data))
</code></pre>
<p>The error I get:</p>
<pre class="lang-none prettyprint-override"><code>TypeError: b'ZGF0YSB0byBiZSBlbmNvZGVk\n' is not JSON serializable
</code></pre>
<p>How can I correctly encode my dictionary containing bytes with JSON?</p>