Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Implement JSON.stringify (subset)

Beyond the Coursehard

Implement `jsonStringify(value)` that serializes a value to a JSON string, matching the built-in `JSON.stringify` for: `null`, numbers, booleans, strings (escaping `\`, `"`, and newlines), arrays, and plain objects. Object keys keep their insertion order. You may assume no functions, `undefined`, or circular references appear at the top level.

Examples

Input: { a: 1, b: [2, 3], c: "x" }
Output: {"a":1,"b":[2,3],"c":"x"}
Input: [1, "two", true, null]
Output: [1,"two",true,null]

Sample tests

Input: jsonStringify({"a":1,"b":[2,3],"c":"x"})
Output: "{\"a\":1,\"b\":[2,3],\"c\":\"x\"}"
Input: jsonStringify([1,"two",true,null])
Output: "[1,\"two\",true,null]"

+ 2 hidden tests run on Submit.

Hints

Common pitfalls
  • Forgetting to escape special characters inside strings produces invalid JSON.
  • Quoting object keys is required — keys are JSON strings too, so reuse the same string serializer.

Learning resources

  • MDN: JSON.stringify()
Approach & explanation (try first)

JSON serialization is a recursive structural walk. Primitives map to their textual forms (strings need escaping), arrays join their serialized elements in brackets, and objects join `"key":value` pairs in braces. Reusing the function for keys and nested values keeps the implementation small and correct.

Loading...
⌘/Ctrl + Enter

Run your code to see results.