json_repair is a Python library that automatically fixes common JSON syntax errors, such as trailing commas, missing quotes, unclosed brackets, and stray text, producing valid JSON output. It can also complete broken structures by closing unclosed arrays and objects, and fill missing values with sensible defaults like empty strings or null.
The library distinguishes itself by handling JSON from large language model outputs, stripping markdown fences, comments, and surrounding prose before parsing. It supports schema-guided repairs, using a JSON Schema to fill missing values, coerce data types, and remove disallowed fields. Additionally, it can process streaming or partial JSON, maintaining a stable best-effort object as new data arrives, and offers strict validation that raises errors on structural issues like duplicate keys.
json_repair provides a command-line interface for fixing JSON files or standard input, with options for strict mode, schema guidance, and output formatting. It also logs each modification made during repair for auditing and debugging, and can skip initial validation to go straight to the repair parser for known-bad input.