Building with LLMs (TypeScript) · Chapter 11 of 24

JSON output

By the end of this chapter you can ask a model for JSON, turn its reply into TypeScript data, cope with the values it leaves empty, read JSON that arrives wrapped in a code fence with a stray comma, and say why a type cast is not a check. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Reliable output
  • 4 tasks
  • TypeScript
  • In the all labs plan

The first 2 chapters of Building with LLMs (TypeScript) are free. This one comes with the all labs plan.

Why you need this

Lata's labels from chapter 9 were one word each: easy to use. Now she wants more from every booking message: where from, where to, how many seats, the customer's name. A sentence like "Krish wants two seats from Bengaluru to Hyderabad" is pleasant to read and useless to a program, which needs seats as the number 2, not somewhere inside a sentence.

JSON is the answer: text that JSON.parse turns into an object in one call. Models write JSON well, because they have read a lot of it. They also write it badly in small, maddening ways, and your code has to know the difference. TypeScript adds a trap of its own: the compiler believes whatever type you give the parsed data.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    json-mode.ts: JSON mode

    Ask lab-chat, with JSON mode on (a response format of type json_object), to turn Diya's message (in the starter) into JSON: the system message says to extract the booking and reply in JSON with the keys from, to, seats, name and phone; the message is the user message.

    Turn the reply into an object and print five lines, one per key in that order: the key, a colon, a space and its value, for example:

    from: Pune

  2. Task 2

    parse-many.ts: many replies, some empty

    For each of the first 12 messages in /workspace/data/messages.jsonl, ask lab-chat in JSON mode for the keys from, to and seats (your instruction in the system message, the customer's text as the user message).

    Print one line per message: its id, the from city, an arrow, the to city, a comma and the seat count, for example M200 Pune -> Goa, 2 seat(s). Where a value is null, print a question mark instead: M201 ? -> ?, ? seat(s)

  3. Task 3

    sloppy.ts: when the JSON is sloppy

    Export a function cleanJson(text: string): unknown that returns the data in a reply wrapped in a Markdown code fence (a first line of three backticks, maybe followed by json, and a last line of three backticks) with trailing commas before a closing brace or bracket. Plain valid JSON must still work, and text that is not JSON must still throw.

    When the file is run, ask lab-chat-sloppy in JSON mode for the keys from, to and seats of Diya's message, and print from cleanJson of the reply one line such as Pune -> Goa, 2 seats

    Put the call to the model inside if (import.meta.main) { ... }

  4. Task 4 · interview question

    Interview: fix-json.ts, read what JSON mode sends back

    fix-json.ts should print, for its three messages, the from city, an arrow, the to city, a colon and the seat count, for example Pune -> Goa: 2 seat(s). A message that does not say how many seats is 1 seat.

    It has three mistakes: two stop it with an error and one prints a wrong line. Fix them until it prints three right lines. Keep the messages and JSON mode.

What you will learn

  • JSON mode: the setting that makes a reply valid JSON.
  • Parsing a reply with JSON.parse, and what null means.
  • What JSON mode does not promise: your keys, your types, and why as Booking does not check them.
  • What breaks: code fences and trailing commas, and how to read them.

More in Reliable output

Build "JSON output" yourself

SafarLine, an intercity bus company, drowns in WhatsApp messages. You build its first AI assistant, in TypeScript. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Building with LLMs (TypeScript) are free. This one comes with the all labs plan.