Watch 10 video solutions for Convert JSON String to Object, a hard level problem. This walkthrough by Tech With Tim has 175,510 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given a string str, return parsed JSON parsedStr. You may assume the str is a valid JSON string hence it only includes strings, numbers, arrays, objects, booleans, and null. str will not include invisible characters and escape characters.
Please solve it without using the built-in JSON.parse method.
Example 1:
Input: str = '{"a":2,"b":[1,2,3]}'
Output: {"a":2,"b":[1,2,3]}
Explanation: Returns the object represented by the JSON string.
Example 2:
Input: str = 'true' Output: true Explanation: Primitive types are valid JSON.
Example 3:
Input: str = '[1,5,"false",{"a":2}]'
Output: [1,5,"false",{"a":2}]
Explanation: Returns the array represented by the JSON string.
Constraints:
str is a valid JSON string1 <= str.length <= 105Problem Overview: You receive a JSON string and must convert it into the equivalent JavaScript object or array without using JSON.parse. The parser must correctly interpret nested objects, arrays, numbers, strings, booleans, and null.
Approach 1: Built-in JSON.parse (Baseline) (O(n) time, O(n) space)
The simplest way to convert a JSON string into an object is calling JSON.parse(str). The runtime scans the entire string once and constructs the corresponding structure in memory. Internally this is implemented with a full JSON parser that tokenizes the input and builds nested objects or arrays. This approach is useful as a reference for correctness and performance, but interview versions of the problem usually forbid built-ins because they want you to implement the parser yourself.
Approach 2: Recursive Descent Parsing (Optimal) (O(n) time, O(n) space)
Treat the JSON string as a stream of characters and parse it using recursive functions. Maintain a pointer i that moves through the string. When you encounter {, recursively parse key-value pairs into an object. When you encounter [, recursively parse elements into an array. Strings are parsed by scanning until the closing quote, while numbers and literals (true, false, null) are recognized by reading sequential characters.
The key insight: each JSON structure naturally maps to recursion. Objects contain nested values, and arrays contain elements that may themselves be arrays or objects. Each character is processed once, giving O(n) time complexity. Recursion depth corresponds to nesting depth, so the space complexity is O(n) in the worst case. This technique is a classic example of string parsing combined with recursion.
Approach 3: Iterative Stack-Based Parser (O(n) time, O(n) space)
An alternative implementation replaces recursion with an explicit stack. Iterate through the string character by character. When you see { or [, push a new container onto the stack. When values are parsed, append them to the container at the top of the stack. Encountering closing brackets } or ] pops the container and attaches it to its parent.
This approach simulates the call stack used in recursive parsing but makes control flow explicit. It’s helpful in environments where recursion depth is limited or when you want more control over the parsing state. The algorithm still scans the string once and maintains containers on a stack, so time complexity is O(n) and auxiliary space is O(n). The structure mirrors common techniques used with a stack to process nested expressions.
Recommended for interviews: Recursive descent parsing is what interviewers typically expect. It clearly demonstrates your understanding of parsing nested structures and managing state with a pointer. Mentioning the built-in solution shows practical awareness, while implementing the recursive parser proves you can design the underlying algorithm.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Built-in JSON.parse | O(n) | O(n) | Real-world code where built-ins are allowed |
| Recursive Descent Parser | O(n) | O(n) | General interview solution for parsing nested JSON |
| Iterative Stack-Based Parser | O(n) | O(n) | When recursion depth is a concern or iterative control is preferred |