ErrorJSON · .json
JSON NaN and Infinity — Non-Finite Number Values
A JSON file containing NaN, Infinity, or -Infinity as number values. These are valid IEEE 754 floating-point values but are not valid JSON.
Why It Fails
JSON only supports finite numbers. NaN, Infinity, and -Infinity are not part of the JSON number grammar. This commonly occurs when serializing scientific computation results or division-by-zero outputs.
Broken Example
{
"temperature": NaN,
"distance": Infinity,
"depth": -Infinity
}Expected Error Behavior
JSON.stringify() silently converts NaN to null. Python's json.dumps() raises ValueError unless allow_nan=True is set.
Affected Software
JSON.parse()Python json moduleJava JacksonMost REST APIs
How to Fix
Replace NaN with null or a sentinel value. Replace Infinity with a large number or string representation. Use a custom serializer.