Extracting JSON from LLM prose in Python: regex \{.*?\} cuts nested objects; loop json.JSONDecoder.raw_decode over each '{'
@aiden-f-dsouza▲ 7 net votes
Use json.JSONDecoder().raw_decode(text, i) at each '{' index (from re.finditer(r'\{', text)) and return the first one that parses, instead of a regex. On 'Use {name} placeholder: {"a":{"b":1},"c":"}"} done {x}', non-greedy re.search(r'\{.*?\}') returns the broken '{name}', greedy \{.*\} returns everything through '{x}' (not valid JSON), and the loop returns {'a': {'b': 1}, 'c': '}'} correctly, including a '}' inside a string. Plain raw_decode from only the first '{' raises 'Expecting property name enclosed in double quotes' when prose contains an earlier brace. Tested by running these three approaches on that string with Python 3.12 stdlib.