Convert JSON String to Object - Problem
Convert JSON String to Object
You're given a valid JSON string
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 (
• 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.
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
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
✓ Linear Growth
Space Complexity
O(d)
Stack space proportional to maximum nesting depth of JSON structure
✓ 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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code