# json_repair — Fix Malformed LLM JSON in Python > json_repair repairs broken JSON from LLMs, logs, and APIs (missing quotes, commas, brackets) and can replace json.loads() with a safe fallback. ## Install Copy the content below into your project: ## Quick Use ```bash pip install json-repair ``` ```python import json_repair bad = '{"users":[{"name":"Ada","role":"admin",}],"ok":tru' obj = json_repair.loads(bad) print(obj) ``` ## Intro json_repair repairs malformed JSON that LLMs often produce (trailing commas, missing quotes, stray prose) and can replace `json.loads()` as a safe fallback. The repo is GitHub-verified at 4,853★ and includes a quick Python example. **Best for:** LLM apps that rely on JSON outputs (tool calls, structured extraction, ETL) and need a reliable fallback parser **Works with:** Python 3.10+; pip/pipx; integrates as a drop-in replacement for json.load(s) **Setup time:** 2–5 minutes ### Key facts (verified) - README states it can be a drop-in fallback for `json.loads()`/`json.load()`. - README lists Python 3.10+ and `pip install json-repair`. - README shows it fixes malformed literals like `tru` → `True` during repair. - GitHub: 4,853 stars · 195 forks; pushed 2026-05-12 (GitHub API verified). ## Main Adopt json_repair as a safety net for structured pipelines: - Keep your normal `json.loads()` path, but swap to `json_repair.loads()` where inputs are model-generated. - Log both the raw input and the repaired output for debugging and regression tests. - If you validate schemas, run repair *before* schema validation so the validator sees a real JSON object. This is especially useful when you require strict JSON for downstream tools (DB inserts, config generation, API calls). ### README excerpt (verbatim) [![PyPI](https://img.shields.io/pypi/v/json-repair)](https://pypi.org/project/json-repair/) ![Python version](https://img.shields.io/badge/python-3.10+-important) [![PyPI downloads](https://img.shields.io/pypi/dm/json-repair)](https://pypi.org/project/json-repair/) [![PyPI Downloads](https://static.pepy.tech/badge/json-repair)](https://pepy.tech/projects/json-repair) [![Github Sponsors](https://img.shields.io/github/sponsors/mangiucugna)](https://github.com/sponsors/mangiucugna) [![GitHub Repo stars](https://img.shields.io/github/stars/mangiucugna/json_repair?style=flat)](https://github.com/mangiucugna/json_repair/stargazers) English | [中文](https://github.com/mangiucugna/json_repair/blob/main/README.zh.md) # 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-repair` or try the [live demo](https://mangiucugna.github.io/json_repair/). ![banner](https://raw.githubusercontent.com/mangiucugna/json_repair/main/banner.png) --- ## Quick example ```python 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](https://github.com/mangiucugna/json_repair) 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](https://notebooklm.google.com/notebook/05312bb3-f6f3-4e49-a99b-bd51db64520b/audio) ## Premium sponsors - [Icana-AI](https://github.com/Icana-AI) Makers of CallCoach, the world's best Call Centre AI Coach. Visit [https://www.icana.ai/](https://www.icana.ai/) - [mjharte](https://github.com/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 **Q: Does it replace json.loads()?** A: It can—README describes using it as a drop-in fallback or replacement for `json.loads()`/`json.load()`. **Q: What inputs is it best for?** A: Model-generated JSON that may include trailing commas, missing quotes, comments, or stray text. **Q: How do I install it?** A: Install with `pip install json-repair` (or `pipx install json-repair`) per README. ## Source & Thanks > Source: https://github.com/mangiucugna/json_repair > License: MIT > GitHub stars: 4,853 · forks: 195 --- ## 快速使用 ```bash pip install json-repair ``` ```python import json_repair bad = '{"users":[{"name":"Ada","role":"admin",}],"ok":tru' obj = json_repair.loads(bad) print(obj) ``` ## 简介 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) [![PyPI](https://img.shields.io/pypi/v/json-repair)](https://pypi.org/project/json-repair/) ![Python version](https://img.shields.io/badge/python-3.10+-important) [![PyPI downloads](https://img.shields.io/pypi/dm/json-repair)](https://pypi.org/project/json-repair/) [![PyPI Downloads](https://static.pepy.tech/badge/json-repair)](https://pepy.tech/projects/json-repair) [![Github Sponsors](https://img.shields.io/github/sponsors/mangiucugna)](https://github.com/sponsors/mangiucugna) [![GitHub Repo stars](https://img.shields.io/github/stars/mangiucugna/json_repair?style=flat)](https://github.com/mangiucugna/json_repair/stargazers) English | [中文](https://github.com/mangiucugna/json_repair/blob/main/README.zh.md) # 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-repair` or try the [live demo](https://mangiucugna.github.io/json_repair/). ![banner](https://raw.githubusercontent.com/mangiucugna/json_repair/main/banner.png) --- ## Quick example ```python 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](https://github.com/mangiucugna/json_repair) 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](https://notebooklm.google.com/notebook/05312bb3-f6f3-4e49-a99b-bd51db64520b/audio) ## Premium sponsors - [Icana-AI](https://github.com/Icana-AI) Makers of CallCoach, the world's best Call Centre AI Coach. Visit [https://www.icana.ai/](https://www.icana.ai/) - [mjharte](https://github.com/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`)。 ## 来源与感谢 > Source: https://github.com/mangiucugna/json_repair > License: MIT > GitHub stars: 4,853 · forks: 195 --- Source: https://tokrepo.com/en/workflows/json-repair-fix-malformed-llm-json-in-python Author: Script Depot