Facebook Pixel

JSON.stringify

JavaScript
medium
25 mins

Implement a custom version of JSON.stringify. This function should serialize a JavaScript object or value into a JSON string. The implementation should mimic the behavior of the native JSON.stringify, including handling of:

  • Primitives (string, number, boolean, null)
  • Arrays and nested arrays
  • Plain objects with nested properties
  • Functions and undefined (ignored in objects, replaced with null in arrays)

Example Inputs & Outputs

JSONStringify("hello") // → '"hello"' JSONStringify({ name: "Alice", age: 30 }) // → '{"name":"Alice","age":30}' JSONStringify([1, "a", true, null]) // → '[1,"a",true,null]' JSONStringify({ a: undefined, b: function() {}, c: 5 }) // → '{"c":5}' JSONStringify([undefined, function() {}, 5]) // → '[null,null,5]'

Constraints & Edge Cases

  • Should handle all basic types: string, number, boolean, null, object, array
  • Functions and undefined:
    • Omitted in objects
    • Converted to null in arrays
  • Throws TypeError on circular references
  • Does not handle special objects like Date, Set, Map, etc.

Companies:

adobe
stripe
paytm

Solve Similar questions 🔥