JSON Parser from Scratch - Problem

Build a complete JSON parser from scratch that can handle all JSON data types including objects, arrays, strings, numbers, booleans, and null values.

Your parser should take a valid JSON string as input and return the parsed data structure. The parser must correctly handle:

  • Objects: {"key": "value"}
  • Arrays: [1, 2, 3]
  • Strings: "hello world"
  • Numbers: 42, -17, 3.14
  • Booleans: true, false
  • Null: null

The parser should properly handle nested structures and whitespace. Return the parsed data in the appropriate format for your language.

Input & Output

Example 1 — Simple Object
$ Input: jsonString = "{\"name\": \"John\", \"age\": 30}"
Output: {"name": "John", "age": 30}
💡 Note: Parse a JSON object with string and number values. The parser identifies the opening brace, parses each key-value pair, and constructs the object.
Example 2 — Array with Mixed Types
$ Input: jsonString = "[1, \"hello\", true, null]"
Output: [1, "hello", true, null]
💡 Note: Parse an array containing different JSON data types: number, string, boolean, and null. Each element is parsed according to its type.
Example 3 — Nested Structure
$ Input: jsonString = "{\"user\": {\"id\": 123, \"active\": true}}"
Output: {"user": {"id": 123, "active": true}}
💡 Note: Parse nested objects where the value of 'user' is itself an object. The recursive parser handles the nesting naturally.

Constraints

  • 1 ≤ jsonString.length ≤ 104
  • Input is guaranteed to be valid JSON
  • May contain nested objects and arrays up to depth 100

Visualization

Tap to expand
JSON Parser from ScratchINPUTALGORITHMRESULTJSON String:{"user": {"name": "Alice","age": 25, "active": true}}1Parse Object2Parse String Keys3Recursive Values4Build StructureParsed Object:user: { name: "Alice", age: 25, active: true}✓ Ready to UseKey Insight:Character-by-character parsing with recursive function calls naturally handles nested JSON structures in O(n) time.TutorialsPoint - JSON Parser from Scratch | Recursive Descent Parser
Asked in
Google 15 Amazon 12 Microsoft 10 Meta 8
28.0K Views
Medium Frequency
~45 min Avg. Time
890 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