Lesson 10
Parsing: Text to Program Values
How parsers build in-memory structures and what can fail before your code runs.
Parsing is the step that turns JSON text into values your program can use—objects, arrays, strings, and numbers in memory. Every language provides a library or built-in for this step; the idea is the same even when the API name differs.
What parsing produces
Input text:
{"id": 7, "tags": ["api", "draft"]}
After a successful parse, your runtime holds a structure you can index: id is a number, tags is a list of strings. Until parsing completes, the text is just a string—unsafe to treat as data.
Typical failure modes
| Problem | Symptom |
|---|---|
| Syntax error | Parser throws or returns error with line/column |
| Invalid UTF-8 | Decode error before JSON rules even apply |
| Huge input | Memory or time limits in the parser |
| Duplicate keys | Last value wins in many parsers—silent data loss |
Parsing validates syntax only. It does not guarantee business meaning (wrong ID, missing field)—that requires schema checks or application logic.
Parse vs validate vs transform
- Parse — Is this valid JSON text?
- Validate (optional) — Does it match a schema or required fields?
- Transform — Map to domain types (dates, decimals, enums)
Keep these stages separate in production pipelines so errors are easy to classify.
Security note
Never evaluate JSON text as code (no eval). Use a dedicated parser. For untrusted input, cap size and depth where your library allows it.
Understanding parsing clarifies why a single stray comma blocks everything downstream—and why fixing syntax always comes first.