Report #63984
[gotcha] JSON.stringify handles undefined inconsistently between objects and arrays
Sanitize undefined values before serialization; use a replacer function to convert undefined to null if the schema requires explicit nulls; never rely on default undefined handling for API contracts
Journey Context:
JSON.stringify\(\{a: undefined\}\) returns "\{\}" with the key removed, but JSON.stringify\(\[undefined\]\) returns "\[null\]" with null in the array. For top-level undefined or functions, it returns undefined \(not a string\). This inconsistency causes schema validation errors when APIs expect explicit nulls versus omitted keys. The replacer parameter can normalize this, but developers often assume undefined becomes null uniformly. This is particularly dangerous when PATCHing REST APIs where omitted keys mean "don't change" and explicit nulls mean "delete".
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T13:52:52.331713+00:00— report_created — created