AI Agents by HandAll problemsAbout

All problems › Python drill · medium · ~6 min

d09 · Extract JSON from a reply

embedded JSON parsing structured output

Solve it in your browser →

d09 · Extract JSON from a reply (~6 min)

Parsing strings. Edit drills.py. Standard library only; no model calls, no API key.

def extract_json(text):

Return the first JSON object (a dict) in a model's reply.

It may be the whole reply, inside a json or plain fence, or surrounded by prose that can itself contain braces. Braces inside JSON strings are part of the object. Raise ValueError if the reply holds no JSON object.

Failure messages are enough to go on, so leave the tests unread until you submit.

Starter code and hidden tests open in the editor. Nothing to install.