Files and data
Understand missing values during JSON serialization
JSON.stringify treats undefined differently in object properties and array slots. Object properties can disappear, while array positions become null so the array’s length and positions remain intact.
Worked example
Node.js example · save as example.mjs and run node example.mjs
console.log(JSON.stringify({name: "sample", missing: undefined}));
console.log(JSON.stringify([1, undefined, 3]));
console.log(JSON.stringify({value: NaN}));How it works
The object output omits missing entirely. The array output retains a middle position with null. NaN is also serialized as null. These conversions can change the meaning of a record if the receiving system distinguishes missing, invalid, and explicitly empty values.
Limits and mistakes
JSON is not a lossless archive of arbitrary JavaScript values. Decide how to represent unsupported values before writing the file. Do not infer that a missing field in saved JSON was necessarily missing from the original in-memory object.
Check the result
Expect {"name":"sample"}, [1,null,3], and {"value":null}. Include round-trip checks for fields where the difference between absent and null affects behavior.
Sources
Reference documentation for the behavior used in this guide: