Command Palette

Search for a command to run...

Hectal
Phase 4: Structured Output
LLM Engineering · Exercise 3 of 6 PreviousNext
Intermediate

Extract the first JSON object from chatty text

Write extract_object(text) that finds the first complete {...} in text by counting braces, ignoring braces inside double-quoted strings (a backslash escapes the next character inside a string), and returns it parsed with json.loads.

Return None when there is no {, when the object never closes (cut off), or when the text between the braces is not valid JSON.

What Check tests

  1. Clean JSON
  2. Chatter before and after
  3. The first of two objects
  4. A closing brace inside a string
  5. Nested objects
  6. Cut off, no JSON, or invalid JSON gives None

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.