Structured data explained
Is NaN Valid JSON? Check What JSON.stringify Actually Sends
Compare bare NaN with a quoted string and a serialized JavaScript value. Inspect the final JSON before applying your numeric schema.

At a glance
Bare NaN and Infinity are invalid numeric tokens in JSON. JavaScript’s ordinary JSON.stringify emits null for a non-finite number. The output can therefore be valid JSON while the intended number is missing.
In this guide
NaN and Infinity cannot appear as bare number tokens in JSON. A JavaScript program can still hold those values, and an ordinary JSON.stringify call can write null where you expected a number. Check the emitted text to distinguish invalid JSON from valid JSON that no longer represents the measurement you expected.
Look at the token before the schema
Start with a small example containing a single value property. If the value’s text is bare NaN, the document does not match the JSON number grammar. The same goes for bare Infinity. A strict parser rejects those tokens even if the source application can represent them internally.
Quote those letters and they are a string. That can make the text valid while changing the kind of value. Before doing this to a real payload, check whether the receiving contract says that a string is allowed and what it would mean there.

Two different failure points
Parser failure
A bare NaN token produces invalid JSON text.
Meaning changed
A numeric runtime NaN can become a valid JSON null when serialized.
For the permitted number tokens, see RFC 8259, section 6, Numbers.
Sources: IETF
Inspect the actual serialized text
In a local Node fixture, the ordinary object with value set to the finite number 42 produced a property containing 42. Setting it to JavaScript’s NaN, Infinity or negative Infinity produced a property containing null. A deliberate null also produced null, while the JavaScript string containing NaN remained a quoted string.
The important diagnostic is the step at which the value changed. Test the original data as a finite number when appropriate, sample the actual serialized text, then parse that text and inspect the resulting value. A debugger showing the object before serialization does not tell you exactly what arrived on the other side.
| JavaScript value | Serialized property | Type parsed from JSON |
|---|---|---|
| 42 | "value":42 | Number |
| NaN | "value":null | Null |
| Infinity or -Infinity | "value":null | Null |
| The string NaN | "value":"NaN" | String |
| null | "value":null | Null |

For ECMAScript serialization behavior, see ECMAScript JSON.stringify.
Decide how the receiver should handle it
If the field is required to represent a real number, find out why the producer cannot supply one. If the domain permits an unavailable measurement, decide with the consumer whether an explicit null, an omitted field or a separate status is the right representation. Avoid silently turning an unknown into zero.
Repeat the round-trip test using that decision. A good fixture checks one finite value and one unavailable case separately, and tests the receiving schema as well as the parser. Then it can show whether the payload is merely parseable or carries the meaning you intended.


Make a short round-trip check
- Capture a small safe runtime fixture.
- Inspect the exact serialized value.
- Parse it and verify the result’s type.
- Confirm what your consumer should accept or reject.
For the related task, see Decide whether null or omission fits the contract.
Frequently asked questions
Can I quote NaN to make it valid JSON?
You can represent those characters as a string. The result is valid JSON with a string, rather than a number.
Is null always evidence that a source number was NaN?
No. A literal null and other non-finite Number values can produce the same output.
Does parsing reconstruct a NaN from null?
No. JSON parsing returns the null value from that token.
How can I preserve why a measurement was absent?
Choose an explicit status or a separate field with the consumer and validate that representation in a round trip.
Read JSON number grammar and precision
Separate numeric syntax from how a serializer handles input.
Compare JSON grammar with the value you actually sendContinue with a related guide
- Read JSON number grammar and precision
Separate numeric syntax from how a serializer handles input.
- Decide whether null or omission fits the contract
Check the receiving schema before adding a placeholder.
- Check the contract’s expected root value
Diagnose the container separately from its properties.
References
- RFC 8259, section 6, Numbers IETF
- ECMAScript JSON.stringify Ecma TC39