JSON Errors / Python
TypeError: Object of type datetime is not JSON serializableHow to Fix: TypeError: Object of type datetime is not JSON serializable
json.dumps() and json.dump() only know how to serialize dict, list, tuple, str, int, float, bool, and None. When they meet anything else, they raise TypeError and name the type: datetime, date, Decimal, set, bytes, a NumPy int64, or a pandas Timestamp.
JSON has no date type, so there is no single correct answer the library could pick for you. You choose the representation - almost always an ISO 8601 string like "2026-11-01T09:30:00+00:00" - and tell json.dumps how to produce it.
Common causes
1. A datetime or date from a database row or model
Rows from sqlite3, psycopg, SQLAlchemy, or the Django ORM come back with datetime and date objects for timestamp columns, and Decimal for numeric columns.
import json, datetime
json.dumps({"created": datetime.datetime.now()})
# TypeError: Object of type datetime is not JSON serializablejson.dumps({"created": datetime.datetime.now().isoformat()})
# '{"created": "2026-11-01T09:30:00.123456"}'2. Decimal, set, or bytes values
The same error with a different type name: "Object of type Decimal is not JSON serializable" (money columns), "set" (deduplicated collections), "bytes" (binary data or undecoded responses). Each needs an explicit conversion: Decimal to str or float, set to a sorted list, bytes to a Base64 or UTF-8 string.
3. NumPy or pandas values
numpy.int64, numpy.float64, numpy arrays, and pandas Timestamp are not Python built-ins, so they fail the same way. Convert with .item() or .tolist() for NumPy, or let pandas write the JSON with DataFrame.to_json(date_format="iso").
How to fix it
import json, datetime, decimal
def to_json(value):
if isinstance(value, (datetime.datetime, datetime.date, datetime.time)):
return value.isoformat()
if isinstance(value, decimal.Decimal):
return str(value) # keeps exact precision
if isinstance(value, set):
return sorted(value)
if isinstance(value, bytes):
return value.decode("utf-8")
raise TypeError(f"Object of type {type(value).__name__} is not JSON serializable")
json.dumps(row, default=to_json)json.dumps(row, default=str)
# datetime becomes "2026-11-01 09:30:00" - str() uses a space, not the ISO "T".
# Fine for logs; use .isoformat() when another program will parse the date.# Django
from django.core.serializers.json import DjangoJSONEncoder
json.dumps(data, cls=DjangoJSONEncoder)
# FastAPI
from fastapi.encoders import jsonable_encoder
json.dumps(jsonable_encoder(data))
# Pydantic v2
model.model_dump_json()Frequently asked questions
Should I use default=str or isoformat()?
Use isoformat() (or a default function that calls it) whenever another program will read the date. str(datetime) gives "2026-11-01 09:30:00" with a space, which JavaScript's Date and many parsers handle inconsistently. isoformat() gives "2026-11-01T09:30:00", the ISO 8601 form every JSON consumer expects.
How do I keep the timezone in the JSON?
Serialize a timezone-aware datetime: datetime.now(timezone.utc).isoformat() produces "2026-11-01T09:30:00+00:00". A naive datetime has no offset, so the reader has to guess - store and send UTC-aware values.
How do I turn the ISO string back into a datetime?
json.loads() returns plain strings, so convert the fields you know are dates: datetime.fromisoformat(data["created"]). Python 3.11+ accepts the full ISO 8601 forms, including a trailing "Z".
Last updated:
Related JSON errors
You might also need
Free and in-browser, like everything on JSON Console. Browse all tools →