Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Convert String to JSON

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Convert String to JSON. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Amazon Echo Show configuration services exchange settings as JSON text. Implement a parser that converts a valid JSON object string into the equivalent nested Python object without calling json.loads or another JSON parsing library.

Formal Specification

Implement parse_json(s), where s is a string containing one complete JSON object. Return a Python dictionary with nested dictionaries, lists, strings, integers, floating-point values, booleans, and None values matching the JSON structure. Ignore whitespace between tokens. You may raise ValueError for malformed input, although test inputs are valid.

Support JSON objects, arrays, strings, numbers, true, false, and null. String escapes must include \", \\, \/, \b, \f, , \r, \t, and \uXXXX.

Constraints

  • 1 <= len(s) <= 10^5
  • The input contains one valid top-level JSON object.
  • Nesting depth is at most 100.
  • Numbers follow JSON syntax and fit in Python numeric types.
  • Do not use a JSON parsing library.

Function Signature

def parse_json(s):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output