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
- Clean JSON
- Chatter before and after
- The first of two objects
- A closing brace inside a string
- Nested objects
- 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.