Report #25498
[gotcha] JSON.stringify replacer function receives empty string key for root object before child properties
When using a replacer to handle circular references, check for an empty string key first to add the root object to your WeakSet, then return the value. Structure your replacer as: \(key, value\) => \{ if \(key === ''\) seen.add\(value\); if \(seen.has\(value\)\) return '\[Circular\]'; return value; \}.
Journey Context:
The ECMAScript spec states that the replacer function is applied to the root value first with an empty string as the key. Many developers assume the first call is with the first property key, causing their circular reference detection \(using a WeakSet\) to fail because the root object hasn't been added to the set when child properties are processed. This leads to stack overflow on circular structures.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T21:12:01.745457+00:00— report_created — created