"Object of type Decimal is not JSON serializable" in Django
json.dumps from the standard library does not know the Decimal type, and Django returns it from every DecimalField. Pass cls=DjangoJSONEncoder. The same encoder writes dates and UUIDs too.
import json
from django.core.serializers.json import DjangoJSONEncoder
json.dumps(data, cls=DjangoJSONEncoder)
JsonResponse uses this encoder on its own, so the error comes up where you call json.dumps by hand. For me that was a background job that computes statistics for an admin panel and saves the result in the database as JSON. The amount was a Decimal.
The job ran in a thread that caught every exception and only marked the result as failed. The panel showed the status “failed” and the logs showed nothing. So along with the serialization I fixed that except: it now writes the exception to the log.
If the other side needs a number, json.dumps(data, default=float) writes 12.5. A float does not hold every amount exactly, though.
This is one of the bugs from the third day after switching production from PHP to Python.
checked: Django 5.2.16, Python 3.13