CheckMySchema

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.

    Two plain study cards and a pencil sit beside a closed laptop.
    Track the original runtime value and the serialized data as different observations.
    CheckMySchemaCheckMySchema

    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.

    Compare JSON grammar with the value you actually send

    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 cards compare a bare NaN token, which fails parsing, to NaN within a JSON string.
    A parser and a numeric schema answer different questions.

    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.

    Observed Node fixture for the value property of an ordinary object.
    JavaScript valueSerialized propertyType parsed from JSON
    42"value":42Number
    NaN"value":nullNull
    Infinity or -Infinity"value":nullNull
    The string NaN"value":"NaN"String
    null"value":nullNull
    The 42 value stays numeric while the NaN and literal null examples both become JSON null.
    Inspect the runtime value before serialization if the two null outputs need different meanings.

    For ECMAScript serialization behavior, see ECMAScript JSON.stringify.

    Sources: Ecma TC39 · IETF

    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.

    Three rows distinguish a present null property, an omitted property, and an agreed description of absence.
    Validate the representation the receiving contract has explicitly accepted.
    Two teal notes and a plain pale card are laid out beside a keyboard.
    Record the missing reason explicitly only if the receiving contract supports it.

    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.

    Sources: IETF · Ecma TC39

    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.

    CheckMySchemaCheckMySchema

    Read JSON number grammar and precision

    Separate numeric syntax from how a serializer handles input.

    Compare JSON grammar with the value you actually send

    Continue with a related guide

    References