简介
json_repair 用来修复 LLM 常见的 JSON 破损问题(多余逗号、缺引号、夹杂解释文字等),可以作为 json.loads() 的安全兜底。仓库 GitHub 已验证 4,853★,并提供快速 Python 示例。
最适合: 依赖 JSON 输出的 LLM 应用(结构化抽取/工具调用/ETL)并需要稳定兜底解析的团队
适配: Python 3.10+;pip/pipx 安装;可作为 json.load(s) 的替代
配置时间: 2–5 分钟
关键事实(已验证)
- README 说明可作为
json.loads()/json.load()的兜底替代。 - README 标注 Python 3.10+,并给出
pip install json-repair。 - README 示例展示修复
tru等破损字面量并返回可用对象。 - GitHub:4,853 stars · 195 forks;最近更新 2026-05-12(GitHub API 验证)。
正文
把 json_repair 当作结构化流程的“安全网”:
- 正常情况下仍然走
json.loads(),但在模型输出路径上改用json_repair.loads()更稳。 - 建议同时记录原始输入与修复后的输出,方便排查与写回归测试。
- 如果你做 schema 校验,通常先 repair,再校验,让校验器面对真正的 JSON 对象。
当你的下游是数据库写入、配置生成或 API 调用时,这种兜底能显著减少因为格式问题导致的失败。
README 原文节选(verbatim)
English | 中文
json_repair
Repair malformed JSON from LLMs, APIs, logs, and user input in Python.
- Fix missing quotes, commas, brackets, comments, stray prose, and truncated values.
- Use it as a drop-in fallback for
json.loads()or as a schema-guided repair step. - Install with
pip install json-repairor try the live demo.

Quick example
import json_repair
bad_json = '{"users":[{"name":"Ada","role":"admin",}],"ok":tru'
decoded_object = json_repair.loads(bad_json)
# {'users': [{'name': 'Ada', 'role': 'admin'}], 'ok': True}If json_repair saves you time, star the repository so more people can find it.
Demo
If you are unsure whether this library will fix your specific problem, or simply want your JSON validated online, try one of these:
- Live demo: https://mangiucugna.github.io/json_repair/
- Audio overview: NotebookLM introduction
Premium sponsors
- Icana-AI Makers of CallCoach, the world's best Call Centre AI Coach. Visit https://www.icana.ai/
- mjharte
Think about sponsoring this library!
This library is free for everyone and is maintained as a side project, so if it helps your work, consider becoming a sponsor: https://github.com/sponsors/mangiucugna
Motivation
Some LLMs are a bit iffy when it comes to returning well formed JSON data, sometimes they skip a parentheses and sometimes they add some words in it, because that's what an LLM does. Luckily, the mistakes LLMs make are simple enough to be fixed without destroying the content.
FAQ
能直接替代 json.loads() 吗?
答:可以:README 说明它可作为 json.loads()/json.load() 的兜底或替代。
适合修哪些输入? 答:适合模型生成的 JSON:可能带多余逗号、缺引号、注释或夹杂自然语言。
如何安装?
答:按 README:pip install json-repair(也可用 pipx install json-repair)。