FrontendInterviews.dev

Loading problem…

69. JSON.parse - Simple

Medium•
Acceptance: 85.71%
•
🔓3/3 Pro unlocks today

Implement a simplified version of JSON.parse that converts JSON strings back to JavaScript values.

Given a JSON string, parse it and return the corresponding JavaScript value. Your implementation should handle:

  • Primitives: numbers, strings, booleans, null
  • Arrays: arrays of any supported types
  • Objects: plain objects with string keys

Requirements

1. Basic Functionality

  • Parse numbers (integers and floats)
  • Parse strings with proper unescaping
  • Parse booleans ("true" and "false")
  • Parse null
  • Parse arrays (comma-separated values in brackets)
  • Parse objects (comma-separated key-value pairs in braces)

Example Usage

parse('42');                    // 42
parse('"hello"');               // "hello"
parse('true');                  // true
parse('null');                  // null
parse('[1,2,3]');              // [1, 2, 3]
parse('{"a":1,"b":2}');        // {a: 1, b: 2}

Real-World Context

This problem models real JSON parsing:

  • API responses: Parse JSON data from HTTP responses
  • Data storage: Deserialize data from localStorage or databases
  • Configuration parsing: Load JSON configuration files
  • Data transfer: Parse JSON data received over network

Constraints

  • 1 <= input.length <= 1000
  • Input is valid JSON string
  • Handle numbers, strings, booleans, null
  • Handle arrays and objects
  • Properly unescape special characters in strings
  • Handle nested structures up to 3 levels