Report #38083
[gotcha] json.dumps outputs non-compliant JSON with NaN/Infinity by default
Explicitly set \`allow\_nan=False\` in \`json.dumps\`/\`dump\` to enforce strict RFC 8259 compliance and raise ValueError on non-finite floats instead of silently emitting invalid JavaScript literals.
Journey Context:
Python's json module defaults to emitting JavaScript-compatible \`NaN\`, \`Infinity\`, and \`-Infinity\` literals for historical reasons, violating RFC 8259. This causes silent interoperability failures when strict JSON parsers \(e.g., PostgreSQL JSONB, standard Java parsers, Go's encoding/json\) reject the payload. Many developers assume 'JSON' implies strict compliance and only discover the issue in production when downstream systems reject payloads. Post-validation is fragile because the invalid tokens are already serialized. Disallowing NaN at the serialization boundary forces explicit handling of non-finite floats \(mapping to None, 0, or strings\) before they poison downstream systems.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T18:24:03.219692+00:00— report_created — created