Convert JSON String to Object - Problem

Given a string str, return the parsed JSON object parsedStr. You may assume the str is a valid JSON string that only includes strings, numbers, arrays, objects, booleans, and null. The string will not include invisible characters and escape characters.

Please solve it without using the built-in JSON.parse method.

Input & Output

Example 1 — Simple Object
$ Input: str = "{\"name\":\"John\",\"age\":30}"
Output: {"name":"John","age":30}
💡 Note: Parse JSON object with string and number values. The parser identifies the object structure and extracts key-value pairs.
Example 2 — Array with Mixed Types
$ Input: str = "[1,true,null,\"hello\"]"
Output: [1,true,null,"hello"]
💡 Note: Parse JSON array containing different data types: number, boolean, null, and string.
Example 3 — Simple String
$ Input: str = "\"hello world\""
Output: hello world
💡 Note: Parse a simple JSON string by removing the surrounding quotes.

Constraints

  • 1 ≤ str.length ≤ 104
  • str is a valid JSON string
  • str contains only printable ASCII characters
  • No escape sequences in strings

Visualization

Tap to expand
INPUTPARSING STEPSRESULT{"name":"John","age":30}JSON StringNeed to parse withoutJSON.parse()1Detect Object{ found → parseObject()2Parse Key-Value"name":"John"3Continue Parsing"age":304Build ObjectReturn complete structureJavaScript Object{ name: "John", age: 30}Key Insight:Use recursive descent parsing - each JSON construct (object, array, string, number) has its own dedicated parser function that calls parseValue() for nested elements.This creates clean, maintainable code that naturally handles arbitrary nesting depth.TutorialsPoint - Convert JSON String to Object | Recursive Descent Parser
Asked in
Google 15 Facebook 12 Amazon 8 Microsoft 6
23.4K Views
Medium 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