Convert a Python datetime to a string with .isoformat() before passing it to json.dumps(). When reading the record, parse the timestamp field with datetime.fromisoformat(). Include an offset for an event that represents an instant, and keep calendar-only values such as due dates as date strings.
Serialize with isoformat and parse the result
import json
from datetime import datetime, timezone
event = datetime(2026, 4, 21, 17, 5, tzinfo=timezone.utc)
payload = {"observed_at": event.isoformat(), "status": "received"}
encoded = json.dumps(payload, allow_nan=False)
decoded = json.loads(encoded)
restored = datetime.fromisoformat(decoded["observed_at"])
assert restored == event
The serialized field contains an offset of +00:00, which identifies this UTC instant. The JSON decoder returns a Python string for that field. The application turns that string into a datetime because it knows the field contract. This narrow conversion leaves unrelated text fields alone.
If you pass a datetime object directly to json.dumps(), the standard encoder normally raises TypeError. That error is useful because it exposes an undefined output contract. Giving the encoder default=str for all unsupported objects can turn mistakes into unexplained strings. It also makes it harder to distinguish timestamps, paths, decimal values and accidental object representations.
Match the receiver’s timestamp format
Decide whether the consumer expects a UTC suffix such as Z, a numeric offset such as +00:00, fractional seconds, or exactly second precision. Check the receiver’s parsing rules. A format that Python can parse is not automatically acceptable to every language or API. If a protocol requires a particular format, validate against that protocol rather than relying on the parser’s broadest accepted forms.
If an event comes from a named zone and the receiver needs that identity, use a separate field for the name. The offset tells it how to interpret the recorded instant. It does not carry the zone’s future schedule rules. A calendar appointment that recurs at 09:00 should typically keep the local time, recurrence rule and named zone instead of reducing the entire schedule to a fixed UTC clock time.
Treat naive timestamps as a validation issue
An input datetime can omit its timezone or its timezone object can provide no usable offset. Decide whether the field allows that. For an event that must identify an instant, reject naive input and report which field was invalid. For a local promise such as a delivery window, keep a representation that makes its local nature explicit and include the zone or business context needed to resolve it.
A timestamp of 09:00 local time does not describe the same instant as 09:00 UTC in most zones. When the original time has a known offset, convert it with astimezone(timezone.utc) to preserve the instant. If it contains only local clock time, resolve the time zone and daylight saving ambiguity before converting.
Compare decoded values and preserve the input
A string sort gives the expected instant order only under a controlled format and offset policy. Two events with different offsets can have an order by text that differs from their order in time. Decode and compare values when the question is chronological. When storing normalized UTC text for a stable lexicographic order, use consistent precision and format as well.
Keep the original string if exact evidence matters. Serializing a parsed datetime again may change the spelling without changing the instant. A field with Z can be serialized as +00:00. That is compatible with an instant comparison but changes the underlying bytes.
Verify what happens at the edges
Use records with a UTC offset, a nonzero offset, no offset, an impossible date and a fractional second. Confirm which cases the receiver accepts. Compare round-trip objects and verify the exact serialized form if a downstream interface cares about it. If missing or null timestamps are permitted, give them explicit semantics so they cannot be mistaken for midnight or a failed conversion.