FrontendInterviews.dev

Loading problem…

100. JSON.parse - Complex

Hard•

Implement a full-featured JSON parser capable of processing deeply nested structures, complex character escaping, and varied whitespace configuration.

Building a robust parser requires implementing a recursive descent state machine that can tokenize input strings accurately. This challenge tests your ability to handle non-trivial edge cases such as escaped quotes, multi-line strings, and deep structural hierarchies that push the limits of recursive execution.

Requirements

  • Recursive Descent Engine: Develop a parsing system that uses multiple recursive functions to consume structural tokens and build the corresponding JavaScript tree.
  • Universal Type Support: Correctlly parse number (including integers, floats, and negative values), string, boolean, and null from valid JSON formatting.
  • Compound Handling: Recursively assemble complex arrays and objects with arbitrary levels of depth.

Specification Compliance

  • Advanced Escaping: Support the full range of JSON escape sequences, specifically transforming \\\" to " and \\\\ to \ inside string literals.
  • Whitespace Tolerance: Correctlly ignore and skip whitespace characters ( , \n, \r, \t) found between tokens and property definitions.
  • Structural Integrity: Ensure object keys are always parsed as strings and that property-value pairs are separated by colons.
  • Manual Implementation: You are strictly forbidden from using eval(), new Function(), or the native JSON.parse() method.

Example Usage

// 1. Complex Escaping
parse('"path: C:\\\\\\\\Users\\\\\\\\John"'); 
// Result: "path: C:\\Users\\John"

// 2. Deep Nesting
parse('{"a": {"b": {"c": 1}}}'); 
// Result: { a: { b: { c: 1 } } }

// 3. Whitespace & Mixed Types
parse(`{
  "status": true,
  "data": [1, null, "foo"]
}`);
// Result: { status: true, data: [1, null, "foo"] }

Constraints

  • Native prohibition: The use of `JSON.parse`, `eval`, or `new Function` is strictly forbidden.
  • Escape sequences: Must correctly handle un-escaping for `\"`, `\\`, `\n`, `\r`, and `\t` literals.
  • Structural depth: Your implementation should handle deep nesting without hitting stack limits unnecessarily.
  • Number literals: Support standard number formats including negative signs and floating point decimals.
Accepted12/15|Acceptance Rate80.0%