Implement JSON.stringify

HardObjects30 min5 tests

Implement a simplified version of JSON.stringify that handles common JavaScript types.

Requirements

  • Handle: strings, numbers, booleans, null, arrays, and plain objects
  • Strings should be wrapped in double quotes
  • undefined values in objects should be omitted
  • Nested objects and arrays should be handled recursively

Example

jsonStringify({ a: 1, b: "hello", c: [true, null] });
// '{"a":1,"b":"hello","c":[true,null]}'
Hints (4)
  1. Handle each type with if/else: null, string, number, boolean, undefined.
  2. For arrays, recursively stringify each element. Replace undefined with null.
  3. For objects, iterate keys, skip undefined values, and format as "key":value.
  4. Strings need to be wrapped in double quotes.

Topics

  • Objects

Asked at

Google · Meta · Amazon · Uber

Join Us
blur