TypeError: Bytes Object Not JSON Serializable

TypeError: Object of type bytes is not JSON serializable

Quick Answer

You have bytes data that needs conversion before JSON serialization. Decode text bytes with .decode() or base64-encode binary data.

Why This Happens

JSON has no binary type. Bytes must be converted to strings: decode UTF-8 for text, or base64-encode for binary data.

The Problem

import json
data = {'content': b'Hello World'}
json.dumps(data)

The Fix

import json, base64
# For text:
data = {'content': b'Hello World'.decode('utf-8')}
# For binary:
data = {'image': base64.b64encode(binary).decode('ascii')}
json.dumps(data)

Step-by-Step Fix

  1. 1

    Decode text bytes

    Use .decode('utf-8').

  2. 2

    Base64 encode binary

    Use base64.b64encode(data).decode('ascii').

  3. 3

    Use a custom encoder

    Write a JSONEncoder that handles bytes.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free