All questions

JSON.stringify

Premium

JSON.stringify

Implement myJsonStringify(value) — return the JSON text for a JavaScript value, matching the behaviour of the built-in JSON.stringify for the cases it does handle. The exercise is small in surface area but spec-quirky: strings need escaping, special numbers turn into null, and undefined has three completely different behaviours depending on where it appears.

Signature

// Returns the JSON string representation of `value`, or `undefined` if the
// top-level value is one of: undefined, a function, or a symbol.
function myJsonStringify(value: unknown): string | undefined;

Examples

// Primitives — strings get quoted; everything else is its text form.
myJsonStringify('hi');     // '"hi"'
myJsonStringify(42);       // '42'
myJsonStringify(true);     // 'true'
myJsonStringify(null);     // 'null'
// Nested objects — keys are quoted, values are recursed into.
myJsonStringify({ user: { name: 'Ada', age: 36 } });
// '{"user":{"name":"Ada","age":36}}'
// Arrays — undefined / functions / symbols are serialized as the string "null".
// Object values that are undefined / functions / symbols are dropped entirely.
myJsonStringify([1, undefined, () => 1, 3]);   // '[1,null,null,3]'
myJsonStringify({ a: 1, b: undefined, c: 3 }); // '{"a":1,"c":3}'
// Special numbers don't have a JSON form — the spec says serialize as null.
myJsonStringify(NaN);       // 'null'
myJsonStringify(Infinity);  // 'null'
myJsonStringify(-Infinity); // 'null'
// Top-level undefined / function / symbol returns the value `undefined` —
// NOT the four-character string "undefined".
myJsonStringify(undefined);     // undefined
myJsonStringify(() => 1);       // undefined
myJsonStringify(Symbol('s'));   // undefined

Notes

  • Strings need escaping. Inside the output, a literal " becomes \", a literal \ becomes \\, and the control characters \n, \t, \r, \b, \f get their two-character forms. Anything else below U+0020 becomes \uXXXX (four lower-case hex digits).
  • Three different rules for undefined. Top-level returns the value undefined so the caller can decide what to do. As an object value the entire key drops out of the result. As an array item it serializes as 'null' — the position has to be preserved so indices stay aligned.
  • Key order. Arrays serialize in index order. Object keys follow Object.keys order (insertion order for string keys; symbol keys are skipped).
  • Out of scope — don't implement these. The real JSON.stringify also supports a replacer argument, an indent argument for pretty-printing, and a toJSON() method on values (which is how Date becomes an ISO string). Skip all three.
  • Out of scope — assume the input is well-behaved. BigInt and circular references both make the real implementation throw; you can ignore them. Date, Map, Set, and typed arrays will fall through to the object branch and produce {} — that's expected for this exercise.

Unlock the solution & editor

  • Runnable editor + tests

    Solve in the browser with instant Jest feedback.

  • Detailed solutions

    Walkthroughs, edge cases, and complexity notes.

  • Multi-framework variants

    React, Vue, Vanilla, Angular — same question, different stacks.

Upgrade to Premium