Repairing Broken JSON
JSON repair
Part of: Structured JSON Extractor
Sometimes the slice you dug out still won't parse. The model left a trailing comma, or the whole thing is fenced, or a stray character snuck in. Rather than fail the extraction, run a small repair pass : a few cheap, safe fixes that turn almost-JSON into real JSON before json.loads. The breakages you'll actually see - Code fences : the reply is wrapped in three backticks and json. - Trailing commas : {"a": 1, "b": 2,} is legal in Python, illegal in JSON. - Whitespace padding the object on either side. Single quotes and unquoted keys also happen, but repairing those safely is hard (an apostrophe inside a name breaks naive fixes), so leave them to a retry rather than a regex. A small, safe repair function [a-zA-Z] \n", "", text) drop opening fence text = re.sub(r"\n The trailing-comma regex reads: a comma followed by optional whitespace and then a closing } or ], replaced with just the closing bracket. That one line handles the most common JSON breakage from LLMs. Repair, then parse, then know when to quit Return None on failure rather than crashing. None is a signal the caller can act on: log it, retry with the model, or flag the record for a human. A crash in the middle of a ten-th
Challenge: Strip the Trailing Commas