json-repair is a Python library designed to automatically fix malformed or invalid JSON strings, making them parseable by standard JSON parsers. It addresses common issues like missing quotes, trailing commas, and incomplete structures, making it particularly useful for processing data from less strict sources like LLMs or web scraping. The library is actively maintained with frequent minor releases addressing bug fixes and performance improvements. The current version is 0.58.7.
pip install json-repairVerified import paths — ran on the pinned version, not inferred.
The quickstart demonstrates repairing a malformed JSON string using both `repair_json` to get a valid string and `loads` for direct parsing into a Python object.
Carefully review the output when using `schema_repair_mode='salvage'`. Consider if `standard` mode or manual post-processing is more appropriate for strict schema adherence. Always test with representative malformed inputs.
Be aware of potential type coercions when providing a `schema` parameter. If strict validation without modification is needed, consider validating the output separately or using an older version if possible, though this is not recommended. The library's focus is on repair and usability.
Simply call `json_repair.repair_json()` or `json_repair.loads()`. If you are certain the input is invalid and want to skip the initial `json.loads()` attempt for performance reasons, pass `skip_json_loads=True`.
Use `strict=True` when you need stricter validation and clear error messages for structural problems, accepting that some malformed JSON that would otherwise be repaired will instead raise an error. For maximum repair capability, omit `strict=True` (default is `False`).
Upgrade to version 0.58.5 or newer for substantial performance gains when repairing JSON.
Ensure the package is installed as `pip install json-repair` and imported as `import json_repair`.
Ensure the input passed to `json_repair.repair_json()` or `json_repair.loads()` is always a string. Convert the input to a string using `str()` if necessary.
Either ensure the input JSON adheres to strict validity, or remove `strict=True` from the function call to allow `json-repair` to perform more lenient, heuristic-based repairs.
Prefer using `json_repair.loads(json_string)` directly, as it automatically attempts standard `json.loads()` first and falls back to repair if decoding fails, eliminating the need for a manual `try-except` block for `json.JSONDecodeError`.