NaN and Infinity are not valid JSON
JSON has no way to write NaN, Infinity or -Infinity. Here is why, what happens when you try, and what to use instead.
Open the JSON formatterWhat the error looks like
Different engines word this differently. You may see any of these:
Why it happens
- The JSON number grammar covers only finite decimal numbers. There is no spelling for NaN or for either infinity.
- JSON.stringify already knows this and quietly writes null for all three, so the value is usually lost before anyone notices.
- Python's json module is the usual source of an actual NaN in a file: by default it writes NaN and Infinity, which are not valid JSON and will not parse elsewhere.
Example
{
"score": NaN,
"limit": Infinity
}
{
"score": null,
"limit": null
}
How this tool reports it
The formatter reports each one and offers to replace it with null, which is what JSON.stringify would have written anyway.
Paste your JSON into the formatter to see the exact line and column, or use the validator if you only need to check it.
Worth knowing
- In Python, pass allow_nan=False to json.dumps to be told about the problem rather than silently producing an invalid document.
- If the distinction matters to your consumer, encode it explicitly, for example {"value": null, "reason": "not-a-number"}.