CODING CHALLENGE · N°40

JSON Parser (Recursive Descent)

Hard ParsingRecursionStrings

Write the parser behind every API and config file: turn a JSON string into native objects, arrays, numbers, strings, booleans and null — by hand, without the built-in JSON library. Recursive descent mirrors the grammar exactly. Solve it in Python or TypeScript, with hidden tests.

The problem

Implement parse_json(text): parse a JSON string into the language’s native values — objects (dicts/maps), arrays (lists), integers, strings, true/false, and nullwithout calling the built-in JSON parser. Support nesting and arbitrary whitespace between tokens. (For this exercise, numbers are integers and strings contain no escape sequences.)

EXAMPLE 1
Input text = '{"a":[1,2,true],"b":null}'
Output {"a": [1, 2, True], "b": None}
nested array and keywords
EXAMPLE 2
Input text = '[1, -2, 3]'
Output [1, -2, 3]
whitespace and a negative number
CONSTRAINTS
  • Do not use json.loads / JSON.parse — build the parser yourself.
  • Support objects, arrays, integers (with optional leading minus), unescaped strings, true/false/null, and whitespace.
  • Recursive descent: one function per grammar rule (value, object, array, string, number), sharing a position cursor.
SOLVE IT YOURSELF

Your turn — write it

Edit the stub, hit Run (or ⌘/Ctrl + Enter), and watch the hidden tests. Stuck? the hints are right above and Reveal solution is one click away.

YOUR TASK

Implement parse_json(text) with a position cursor and one function per rule. value() dispatches on the next non-space character; object()/array() loop over comma-separated items; string()/number() read literals. Return the fully built native value.

HINTS — 4 IDEAS
  1. Keep an index pos and a skip() that advances past spaces/tabs/newlines. Call it before reading each token.
  2. value(): peek the next char — "{" → object, "[" → array, "\"" → string, "t"/"f"/"n" → keyword, else a number.
  3. For an object, consume "{", then repeatedly read a string key, a ":", and a value, separated by ",", until "}". Arrays are the same without keys.
  4. A number runs from an optional "-" through the digits; stop at the first non-digit (a comma, bracket, or brace).
CPython · WebAssembly

Explore the topic

See this challenge alongside everything else on the same subject — handbooks, system designs, algorithms and tools, in one place.