Convert Object to JSON String - Problem
Convert Object to JSON String

In this challenge, you need to implement your own JSON serializer! Given any JavaScript value (string, number, array, object, boolean, or null), you must return a valid JSON string representation of that value.

The goal is to understand how JSON serialization works under the hood by building it from scratch. Your output should be a properly formatted JSON string with:

No extra spaces - keep it compact
Proper escaping - handle special characters in strings
Consistent key ordering - use the same order as Object.keys()
Correct type handling - numbers, booleans, null, strings, arrays, and objects

This is essentially building your own JSON.stringify() method! You'll gain deep insights into how modern browsers and Node.js handle JSON serialization.

Note: You cannot use the built-in JSON.stringify() method.

Input & Output

example_1.py — Simple Object
$ Input: {"name": "Alice", "age": 30}
Output: "{\"name\":\"Alice\",\"age\":30}"
💡 Note: Object with string and number values. Keys are quoted, strings are escaped, and numbers are converted directly. No extra spaces.
example_2.py — Array with Mixed Types
$ Input: [1, true, null, "hello"]
Output: "[1,true,null,\"hello\"]"
💡 Note: Array containing different types: number (1), boolean (true), null, and string. Each type is serialized according to JSON rules.
example_3.py — Nested Structure
$ Input: {"users": [{"id": 1, "active": false}], "count": 1}
Output: "{\"users\":[{\"id\":1,\"active\":false}],\"count\":1}"
💡 Note: Complex nested structure with object containing array of objects. Demonstrates recursive serialization maintaining proper JSON format.

Visualization

Tap to expand
JavaScriptValue(any type)TypeDetectionPrimitiveStringComplextoString()escape + quoterecurse + joinJSONStringRecursion fornested elements42"text"[1,2,3]{a:1}"42""\"text\"""[1,2,3]""{\"a\":1}"
Understanding the Visualization
1
Input Analysis
Examine the input value and determine its JavaScript type
2
Type Routing
Route to appropriate conversion logic based on the detected type
3
Primitive Conversion
For primitives (null, boolean, number), convert directly to string
4
String Escaping
For strings, add quotes and escape special characters
5
Recursive Processing
For arrays/objects, recursively process each element/property
6
Structure Assembly
Combine processed parts with appropriate delimiters and brackets
Key Takeaway
🎯 Key Insight: JSON serialization is naturally recursive - each type has specific rules, and complex structures (arrays/objects) simply apply the same rules to their contents, making the solution both elegant and efficient.

Time & Space Complexity

Time Complexity
⏱️
O(n)

Where n is the total number of values in the structure. Each value is visited exactly once.

n
2n
Linear Growth
Space Complexity
O(d)

Where d is the maximum depth of nesting due to recursive call stack. Additionally, temporary arrays for join operations.

n
2n
Linear Space

Constraints

  • The input can be any valid JavaScript value
  • Strings may contain special characters that need escaping (\, ", \n, \r, \t)
  • Objects can have up to 103 properties
  • Arrays can have up to 103 elements
  • Maximum nesting depth is 100 levels
  • Cannot use JSON.stringify() or similar built-in methods
  • Must maintain the same key ordering as Object.keys()
  • Output must be valid JSON with no extra whitespace
Asked in
Google 45 Meta 38 Amazon 32 Netflix 28 Microsoft 22
82.6K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen