Guide
JSON: null vs missing fields vs undefined
Distinguish an explicit null, an absent property, and JavaScript undefined to avoid silent changes in APIs and serialization.
by Tools in a Tab · Published on · Reviewed on
Short answer
In JSON, null is an explicit value, while a missing field is not part of the
object at all. undefined is not JSON syntax: it belongs to JavaScript and
some other environments. Confusing these three states during serialization can
remove object properties or change array entries.
Three different cases
{ "middleName": null }
This document says that the property exists and its value is null. By
contrast:
{}
contains no information about middleName. What that absence means depends on
the API contract: it may mean “do not modify,” “use a default,” or an error.
JSON itself does not define that application meaning.
This text is not valid JSON:
{ "middleName": undefined }
The JSON validator rejects it because undefined
is not an allowed literal.
What JavaScript JSON.stringify does
Serialization treats object properties and array positions differently:
JSON.stringify({ a: undefined, b: null });
// {"b":null}
JSON.stringify([undefined, null]);
// [null,null]
An object property with an undefined value is omitted. In an array, a
non-serializable position becomes null so that the length is retained.
Functions and symbols can cause corresponding outcomes depending on their
position.
Inspect the final text being sent, not only the JavaScript object that existed before serialization.
Design the API contract explicitly
Define each permitted state separately:
- Missing field: no decision about that data was sent.
- Field set to
null: an explicit null value was sent. - Field with a value: concrete data was sent.
Some partial-update APIs treat absence as “leave unchanged” and null as
“clear the value,” but this is not universal. Document and test the behavior.
If null is forbidden, express that rule in the schema or server validation.
Common mistakes
- Writing
"undefined"and believing it represents absence; it is only a string. - Replacing unknown values with
nullwithout checking whether it means deletion. - Testing only
object.field == null, which groupsnullandundefinedunder JavaScript’s loose equality. - Serializing before detecting omitted properties and losing the evidence.
- Expecting a converter to restore a field that never reached the JSON text.
RFC 8259 lists null
as a JSON value and does not include undefined. The normative
JSON.stringify algorithm
defines JavaScript’s omissions and substitutions. Validate the final document
and make absence semantics an explicit part of the contract.