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

You're given a valid JSON string str and need to parse it into a JavaScript object without using the built-in JSON.parse() method.

The JSON string can contain:
Strings (wrapped in double quotes)
Numbers (integers and floats)
Arrays (comma-separated values in square brackets)
Objects (key-value pairs in curly braces)
Booleans (true or false)
Null values

Note: The input is guaranteed to be valid JSON with no escape characters or invisible characters.

Goal: Implement your own JSON parser that correctly handles nested structures and returns the equivalent JavaScript object.

Input & Output

example_1.py — Simple Object
$ Input: str = "{\"name\":\"John\",\"age\":30,\"active\":true}"
Output: {'name': 'John', 'age': 30, 'active': True}
💡 Note: Parse a simple JSON object with string, number, and boolean values. Each key-value pair is extracted and converted to appropriate Python types.
example_2.py — Nested Array
$ Input: str = "{\"users\":[{\"id\":1,\"name\":\"Alice\"},{\"id\":2,\"name\":\"Bob\"}]}"
Output: {'users': [{'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}]}
💡 Note: Parse nested structures with an array containing objects. The parser recursively handles each level of nesting to build the complete structure.
example_3.py — Mixed Types
$ Input: str = "[\"hello\", 42, true, null, {\"key\": [1, 2, 3]}]"
Output: ['hello', 42, True, None, {'key': [1, 2, 3]}]
💡 Note: Parse an array containing all possible JSON value types: string, number, boolean, null, and nested object with array. Each type is correctly identified and converted.

Visualization

Tap to expand
STARTOBJECTARRAYVALUESTRINGNUMBERBOOLEANNULL'{' detected'[' detectedother charsrecursivecallsrecursivecallsJSON Parser State MachineParser transitions between states based on current character and recursively processes nested structures{"name": "John", "data": [1, 2, {"nested": true}]}Input JSON string processed character by character
Understanding the Visualization
1
Character Analysis
Look at current character to determine value type: {=object, [=array, "=string, etc.
2
State Transition
Call appropriate parser function based on the detected type
3
Recursive Processing
For nested structures, recursively parse inner values
4
Value Assembly
Combine parsed values into final data structure
Key Takeaway
🎯 Key Insight: A recursive descent parser with character-by-character processing provides the most efficient and accurate JSON parsing solution with O(n) time complexity.

Time & Space Complexity

Time Complexity
⏱️
O(n)

Single pass through each character in the JSON string

n
2n
Linear Growth
Space Complexity
O(d)

Stack space proportional to maximum nesting depth of JSON structure

n
2n
Linear Space

Constraints

  • 1 ≤ str.length ≤ 105
  • str is a valid JSON string
  • No escape characters or invisible characters
  • May contain nested objects and arrays up to 100 levels deep
Asked in
Google 42 Amazon 38 Meta 29 Microsoft 25 Netflix 18
31.4K Views
High Frequency
~35 min Avg. Time
892 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