Report #84482
[gotcha] JSON serialization producing invalid JSON with NaN or Infinity
Pre-process floats to handle inf/nan before serialization, or use allow\_nan=False to force strict JSON compliance and catch errors early. Never send output with allow\_nan=True \(the default\) to strict JSON parsers \(JavaScript, Go, etc.\) as it produces non-compliant JavaScript literals.
Journey Context:
Python's json module defaults to allow\_nan=True, which serializes float\('nan'\) and float\('inf'\) as JavaScript literals NaN and Infinity. However, RFC 8259 does not permit these values in standard JSON, causing parsing failures in strict parsers \(PostgreSQL JSONB, Go encoding/json, JavaScript JSON.parse in some contexts\). The documentation notes that allow\_nan=False will raise a ValueError instead of producing invalid JSON.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T00:23:43.195521+00:00— report_created — created