Design a JSON/XML Parser
🧾 What is a document parser? It is the code that turns text like
{"name": "Ada", "tags": ["math"]}into objects your program can walk. It checks that the text follows the format's rules, and it builds a tree: objects that hold fields, arrays that hold items, and plain values at the leaves. Every web service, config loader and data import has one inside.
Interviewers ask this as "design a JSON parser", "design an XML parser", or "design a parser that supports several formats". They are one design. Each format gets its own small parser built on a shared reading layer (a tokenizer and a cursor), every parser uses recursive descent to follow the nesting, and all of them produce the same tree, so adding a format never touches the code that uses the tree. We build JSON fully and a small XML subset as the second format, which is exactly where the "swappable formats" question comes from.
Requirements
"Design a JSON parser. Later we will also need XML, and maybe other formats."
The problem looks like an algorithm question, but the interviewer is just as interested in the boundaries: what the tokenizer knows, what the parser knows, and what the rest of the system sees.
Clarifying Questions
You: Do we follow the full JSON standard, or a simplified version?
Interviewer: Standard JSON: objects, arrays, strings with escapes, numbers, true, false, null. One difference: reject an object that repeats a key. The standard leaves that case open, and our downstream code does not want to guess which value wins.
You: How much XML?
Interviewer: A small subset: elements, attributes, text and the five standard entities like &. No prolog, comments, CDATA, namespaces or DTDs.
You: What should the parser return?
Interviewer: A tree that the rest of the code can walk without knowing which format it came from. For the tests, print that tree as compact JSON. An XML element becomes an object with tag, attrs and children.
You: How should numbers be stored? A float would turn a long ID like 12345678901234567890 into something else.
Interviewer: Good catch. Don't lose anything: print every number exactly as it was written.
You: What happens on invalid input?
Interviewer: Return "ERROR" for now. Good error messages would be nice later.
You: How big are the documents? Can we read the whole text into memory?
Interviewer: A few kilobytes each, so yes. We may ask about huge files at the end.
You: Can we use the language's built-in JSON library?
Interviewer: No, that is the part we want to see.
The answer about numbers is a small decision with a big effect: the tree stores a number's text, not a float. The answer about the tree is the big one: since both formats must produce the same tree, the tree is designed first, and each format's parser is just a different way to build it.
💡 Interview tip: Ask what the parser returns before you write any parsing code. "A tree of node classes", "native dicts and lists" and "a stream of events" lead to three different designs, and the interviewer usually has one in mind.
Final Requirements
parse(format, text)parses a document in the named format ("JSON"or"XML") and returns the tree as canonical JSON, or"ERROR"if the format is unknown or the text is invalid.- JSON follows the standard grammar: exactly one value, with optional whitespace; numbers, strings and escapes as defined by the standard; no trailing commas; no duplicate keys in one object.
- XML is one root element. Elements may have attributes (quoted values) and content made of text and child elements. The five entities
< > & " 'are decoded. Text that is only whitespace between tags is dropped. - Both formats produce the same tree. An XML element becomes an object
{"tag": ..., "attrs": {...}, "children": [...]}. - Numbers keep the exact text they were written with. Object keys keep their order.
- Adding a new format must not change the existing parsers or the tree.
Out of scope
- Full XML: prologs, comments, CDATA, namespaces, DTDs.
- Non-ASCII text and surrogate pairs in
\uescapes. - Error messages and positions (we come back to this in Extensibility).
- Streaming huge files (also in Extensibility).