Report #16136
[gotcha] JSON.stringify handles undefined differently in objects \(omits key\) versus arrays \(converts to null\), causing data loss or type corruption
Use a replacer function to throw on undefined, or normalize data before serialization; never rely on default undefined handling for arrays
Journey Context:
In objects, properties with undefined values are completely omitted from the JSON string. In arrays, undefined elements are converted to null to preserve indices and length. This asymmetry means \{a: undefined\} becomes '\{\}' while \[undefined\] becomes '\[null\]'. During round-tripping, objects lose keys entirely while arrays mutate types \(undefined becomes null\). This is specified behavior for historical compatibility, but it makes undefined an unsafe serialization value. The robust pattern is a replacer that rejects undefined explicitly, or using a schema validator before stringify to ensure no undefined values exist in arrays.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T01:53:28.417045+00:00— report_created — created