myJSONStringify(value) converts a JavaScript value into a JSON string. The interviewer wants to see you handle all JSON types, escape strings properly, skip non-JSON values, and detect circular references — essentially a recursive tree serializer.
Related deep-dive: Parsing #1 — Building a Simple JSON.stringify()
What is JSON.stringify()?
JSON.stringify(value) converts a JavaScript value into a JSON-formatted string. It's the standard serialization format for web APIs, config files, and data interchange. The native implementation handles primitives, arrays, objects, and has specific rules for what gets skipped or transformed.
Building it from scratch is a recursive tree serializer — you walk the value, emit the correct JSON syntax at each node, and handle edge cases the native implementation defines:
- Primitives — strings get double-quoted and escaped; numbers and booleans emit directly;
nullemits"null" - Arrays — emit
[...]with recursively serialized elements - Objects — emit
{...}with quoted keys and recursively serialized values - Non-JSON values —
undefined, functions, andSymbolkeys/values are skipped (not emitted) in objects and replaced withnullin arrays - Circular references — must be detected and rejected (throws
TypeErrorin native)
The interview tests whether you can handle all these branches correctly. The key edge cases: string escaping (", \, newlines, etc.), distinguishing arrays from plain objects, detecting cycles with a WeakMap/Set, and knowing what JSON considers valid vs. invalid.
Real-world use cases go far beyond building your own serializer. Understanding how JSON.stringify works internally helps you debug serialization bugs, understand why certain values disappear from API payloads, and know when to reach for alternatives like structuredClone or custom serializers.
The Problem
"Implement myJSONStringify(value) that returns a JSON string. It should handle: null, booleans, numbers, strings, arrays, and plain objects. Skip undefined, functions, and Symbol values. Detect circular references."
Thought Process
JSON is a recursive grammar:
- null →
"null" - boolean →
"true"or"false" - number →
String(value)(butNaNandInfinity→"null") - string → wrap in double quotes, escape
",\,\n,\t,\r, etc. - array →
[+ elements joined by,+] - object →
{+ key-value pairs joined by,+}
For circular reference detection, pass a WeakSet of visited objects through the recursion.
Step 1 — Primitives
Loading editor...
Step 2 — Arrays
Loading editor...
Step 3 — Objects
Loading editor...
Step 4 — Edge Cases
undefined in arrays → null. [1, undefined, 3] → [1,null,3].
undefined / function / symbol as object values → skip the key entirely.
NaN / Infinity → null. Both are invalid JSON values.
Circular references: The WeakSet tracks visited objects. On second visit, throw a TypeError — matching the native behavior.
Empty array/object: [] → "[]", {} → "{}".
Full Solution
Loading editor...
What Interviewers Are Testing
- JSON grammar knowledge — what's valid JSON and what isn't
- String escaping — handling double quotes, backslashes, and control characters
- undefined/function/symbol handling — knowing they're skipped in objects, replaced with null in arrays
- Circular reference detection — using a WeakSet for O(1) lookup and automatic GC
- NaN/Infinity → null — the JSON spec says these become null
Complexity
| Time | Space | |
|---|---|---|
| stringify | O(N) — each value visited once | O(N) — output string + recursion stack |
Interview Tips
- State the type-dispatcher upfront — "I'll handle each type: null, boolean, number, string, array, object." This shows structured thinking.
- Handle
nullbeforetypeof—typeof null === 'object'will bite you if you checktypeoffirst. - Use WeakSet, not Set, for visited tracking — mention that WeakSet allows garbage collection if the serialized object is discarded mid-operation.
- Walk through the escape table — "For strings I need to escape at minimum: backslash, double-quote, newline, carriage return, and tab."