FrontendInterviews.dev

Loading problem…

100. JSON.parse - Complex

Hard•
Acceptance: 80.00%
•
🔓3/3 Pro unlocks today

Implement a more complete version of JSON.parse that handles advanced cases including:

  • Deeply nested structures (5+ levels)
  • Complex string escaping (all escape sequences)
  • Numbers (integers, floats, negative, zero, scientific notation)
  • Mixed data types in arrays and objects
  • Whitespace handling
  • Complex nested structures

Given a JSON string, parse it and return the corresponding JavaScript value. Your implementation should handle all basic types and complex nested structures with proper error handling.

Requirements

1. Advanced Functionality

  • Handle deeply nested structures (10+ levels)
  • Properly unescape all special characters and escape sequences
  • Handle all number formats (integers, floats, negative, zero, scientific notation)
  • Preserve object key order
  • Handle mixed types in arrays
  • Proper whitespace handling
  • Better error messages

Example Usage

parse('{"a":{"b":{"c":{"d":{"e":1}}}}}');  // Deep nesting
parse('"text with \"quotes\" and \\backslashes"');  // Complex escaping
parse('[1,"two",true,null,{"x":1}]');      // Mixed types
parse('{"0":"zero","1":"one"}');            // Numeric keys

Real-World Context

This problem models real JSON parsing in production:

  • Complex API responses: Parse deeply nested API data
  • Configuration parsing: Load complex JSON configurations
  • Data deserialization: Restore complex application state
  • Cross-system communication: Parse JSON from different systems

Constraints

  • 1 <= input.length <= 10000
  • Input is valid JSON string
  • Handle nested structures up to 10 levels
  • Properly unescape all escape sequences
  • Handle all number formats
  • Preserve object key order