Here is the snippet that usually starts the argument:
{"ratio": NaN, "max": Infinity, "name": "sensor-4"}
Paste that into a browser console as an object literal and nothing complains. Hand the same text to JSON.parse and it fails. Both reactions are correct, because the two formats are not the same thing. JavaScript objects can hold NaN and Infinity. JSON cannot. JSON numbers are digits, with an optional minus sign, a fraction, and an exponent. There is no token for "not a number" and no token for "too big to count." The parser stops at N, because N starts nothing it recognizes.
That distinction explains almost every version of this bug. A calculation divides by zero, a sensor returns no reading, a model outputs a score that overflowed, and the value slides into a hand-built string or a lenient serializer. Everything downstream looks normal until a strict parser meets it. The fix is never to make the parser accept NaN. The fix is to decide, before serialization, what those values mean in your data.
How to fix NaN and Infinity in JSON without losing the meaning
The simplest repair is null. It works when "no usable number" is all the receiver needs to know. A temperature reading that failed, a ratio that divided by zero, a score that never computed: null says the value is absent, and every JSON parser on earth understands it. Use null when the missing number and the failed number get the same treatment downstream.
Sometimes they do not. A dashboard that averages readings should skip a failed sensor, but a debugging log that records why a calculation failed needs to tell "never measured" apart from "measured and broke." That is the one case where a string earns its keep. Write "NaN" or "Infinity" as a quoted string, and convert it back on the receiving side with a reviver, the second argument to JSON.parse that transforms values as they are read. The string round trips through any parser, and your code decides what it means. The cost is that both sides must agree on the string, and anyone who reads the raw file without your reviver sees text where they expected a number. Document the convention next to the schema, not in a chat message nobody will find.
The third option is to drop the key. JSON.stringify already does this for undefined values, and it is honest for optional fields: if the value is not there, the field is not there. I would not use it for readings in a fixed-shape record, because a missing key and a null value are different shapes to a typed consumer, and a consumer that expects every key will break in a new way. Dropping keys suits sparse metadata, not tables.
The Python trap that ships invalid JSON
Python deserves its own warning, because it hides this bug better than any other language. The standard json module accepts NaN and Infinity by default. Call json.dumps on a dict holding float("nan") and you get text with a bare NaN in it, no error, no warning. Call json.loads on that text and it parses, because Python's parser accepts the same words. Inside Python, the round trip works, which is why it escapes into production.
The moment a JavaScript client, a Go service, or a Java parser meets that text, it fails. Python's allow_nan parameter controls this behavior, and the name misleads people: allow_nan=True is the default, and it means "write invalid JSON and hope nobody notices." Set allow_nan=False and the encoder raises a ValueError instead of emitting bad text. That error is the useful behavior. It forces the choice upstream, where you still know what the value was supposed to be, instead of downstream, where a stranger's parser throws on your data. If you use NumPy, watch for the same trap one layer up: numpy float values that are nan or inf flow into json.dumps the same way, and the fix is the same, sanitize before serialization or set allow_nan=False and handle the error.
On the JavaScript side, the matching habit is a replacer function, the second argument to JSON.stringify. A replacer that maps non-finite numbers to null, or to agreed strings, runs on every value before the text is built. One replacer in the serialization helper beats a dozen ad hoc fixes at call sites, because the bug is a serialization policy, not a one-off typo. The same goes for Go, where encoding/json refuses to encode NaN and Infinity outright and returns an error. Go developers complain about that error, but it is the honest version of Python's silence.
A quick worked example ties it together. Say a service computes a ratio for 4 sensors and one divides by zero. Serializing raw gives {"s1": 0.42, "s2": NaN, "s3": 1.18, "s4": 0.91}, which no strict parser reads. Mapping non-finite values to null gives {"s1": 0.42, "s2": null, "s3": 1.18, "s4": 0.91}, which every parser reads, and the dashboard can skip s2 and flag it. Mapping to strings gives {"s2": "NaN"}, which only helps if the receiver converts it back. The null version is the default I would ship, and the string version is the one I would reach for only when a consumer genuinely needs the distinction.
Paste your broken JSON into the repair tool and it will flag bare NaN and Infinity values and show a valid replacement before you send the payload anywhere else.
Frequently asked questions
Why does JSON.parse fail on NaN and Infinity?
JSON has no NaN or Infinity value. A JSON number is digits with an optional sign, fraction, and exponent. The parser meets the letter N or I, finds no matching token, and throws a SyntaxError.
What should NaN become in valid JSON?
Usually null, or a string such as "NaN" if the receiving code must tell a missing value apart from a failed calculation. Pick one rule, document it, and apply it everywhere.
Why does JSON.stringify turn NaN and Infinity into null?
The JavaScript serializer follows the JSON grammar, which has no way to write those values, so it writes null for them in place. Object properties set to undefined are dropped instead.
Does Python json.dumps with allow_nan=True produce valid JSON?
No. It writes the bare words NaN, Infinity, and -Infinity, which strict parsers reject. It only looks valid inside Python, where json.loads accepts those same words by default.
How do I keep Infinity through a JSON round trip?
Encode it as a string on the way out and convert it back with a reviver on the way in, for example mapping "Infinity" back to the JavaScript Infinity value. Both sides must agree on the string.
Get one practical dev-tools guide a week: subscribe to the newsletter for new JSON guides and parser explainers.
Related reading: How to Fix Truncated JSON From AI Output · Unexpected Token U: You Parsed Undefined, Not JSON · JSON5 vs JSON: What JSON5 Allows That Strict JSON Rejects