JSON Parser (Recursive Descent)
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 null — without 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.)
text = '{"a":[1,2,true],"b":null}'{"a": [1, 2, True], "b": None}text = '[1, -2, 3]'[1, -2, 3]- 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.
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.
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.
- Keep an index
posand askip()that advances past spaces/tabs/newlines. Call it before reading each token. value(): peek the next char — "{" → object, "[" → array, "\"" → string, "t"/"f"/"n" → keyword, else a number.- For an object, consume "{", then repeatedly read a string key, a ":", and a value, separated by ",", until "}". Arrays are the same without keys.
- A number runs from an optional "-" through the digits; stop at the first non-digit (a comma, bracket, or brace).
Explore the topic
See this challenge alongside everything else on the same subject — handbooks, system designs, algorithms and tools, in one place.