freshcrate
Home > Developer Tools > json-repair

json-repair

A package to repair broken json strings

Description

[![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 | [中文](README.zh.md) This simple package can be used to fix an invalid json string. To know all cases in which this package will work, check out the unit test. ![banner](banner.png) --- # Think about sponsoring this library! This library is free for everyone and it's maintained and developed as a side project so, if you find this library useful for your work, consider becoming a sponsor via this link: https://github.com/sponsors/mangiucugna ## 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) --- # Demo If you are unsure if this library will fix your specific problem, or simply want your json validated online, you can visit the demo site on GitHub pages: https://mangiucugna.github.io/json_repair/ Or hear an [audio deepdive generate by Google's NotebookLM](https://notebooklm.google.com/notebook/05312bb3-f6f3-4e49-a99b-bd51db64520b/audio) for an introduction to the module --- # 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. I searched for a lightweight python package that was able to reliably fix this problem but couldn't find any. *So I wrote one* # Supported use cases ### Fixing Syntax Errors in JSON - Missing quotes, misplaced commas, unescaped characters, and incomplete key-value pairs. - Missing quotation marks, improperly formatted values (true, false, null), and repairs corrupted key-value structures. ### Repairing Malformed JSON Arrays and Objects - Incomplete or broken arrays/objects by adding necessary elements (e.g., commas, brackets) or default values (null, ""). - The library can process JSON that includes extra non-JSON characters like comments or improperly placed characters, cleaning them up while maintaining valid structure. ### Auto-Completion for Missing JSON Values - Automatically completes missing values in JSON fields with reasonable defaults (like empty strings or null), ensuring validity. # How to use Install the library with pip pip install json-repair then you can use use it in your code like this from json_repair import repair_json good_json_string = repair_json(bad_json_string) # If the string was super broken this will return an empty string You can use this library to completely replace `json.loads()`: import json_repair decoded_object = json_repair.loads(json_string) or just import json_repair decoded_object = json_repair.repair_json(json_string, return_objects=True) ### Avoid this antipattern Some users of this library adopt the following pattern: obj = {} try: obj = json.loads(string) except json.JSONDecodeError as e: obj = json_repair.loads(string) ... This is wasteful because `json_repair` already does that strict `json.loads()` check for you by default. The normal flow is: - try the built-in `json.loads()` / `json.load()` first - if that succeeds, return the decoded object - if that fails, run the repair parser Use the default call unless you explicitly want to skip that initial validation step: ```python import json_repair decoded_object = json_repair.loads(json_string) ``` ### Read json from a file or file descriptor JSON repair provides also a drop-in replacement for `json.load()`: import json_repair try: file_descriptor = open(fname, 'rb') except OSError: ... with file_descriptor: decoded_object = json_repair.load(file_descriptor) and another method to read from a file: import json_repair try: decoded_object = json_repair.from_file(json_file) except OSError: ... except IOError: ... Keep in mind that the library will not catch any IO-related exception and those will need to be managed by you ### Non-Latin characters When working with non-Latin characters (such as Chinese, Japanese, or Korean), you need to pass `ensure_ascii=False` to `repair_json()` in order to preserve the non-Latin characters in the output. Here's an example using Chinese character

Release History

VersionChangesUrgencyDate
0.59.4Imported from PyPI (0.59.4)Low4/21/2026
v0.59.4# Fixed - Fix #193, improve handling of various exceptions and corner cases related to code fences when the LLM outputs code # Think about sponsoring this library! This library is free for everyone and it's maintained and developed as a side project so, if you find this library useful for your work, consider offering me a beer via this link: https://github.com/sponsors/mangiucugnaHigh4/15/2026
v0.59.3# Fixed - Fix #192, preserve literal fenced snippets in multiline strings - Throw a `ValueError` in case the library hits a `RecursionError` # Think about sponsoring this library! This library is free for everyone and it's maintained and developed as a side project so, if you find this library useful for your work, consider offering me a beer via this link: https://github.com/sponsors/mangiucugnaMedium4/14/2026
v0.59.2# Fixed - Fix #189, when an LLM emits unescaped JSON like `{"blank_1": "..."}` inside a quoted explanation string. Previously the parser could split the parent string, invent extra object members, or even change the top-level shape when another member followed. # Think about sponsoring this library! This library is free for everyone and it's maintained and developed as a side project so, if you find this library useful for your work, consider offering me a beer via this link: https://gitMedium4/11/2026
v0.59.1# Fixed - Fix #191, 0.59.0 introduced a regression when the json was preceded by text with parentheses # Think about sponsoring this library! This library is free for everyone and it's maintained and developed as a side project so, if you find this library useful for your work, consider offering me a beer via this link: https://github.com/sponsors/mangiucugnaMedium4/11/2026
v0.59.0# Added - Fix #190, support python style array syntax `()` # Fixed - Prevent recursion depth error in `parse_comment` # Think about sponsoring this library! This library is free for everyone and it's maintained and developed as a side project so, if you find this library useful for your work, consider offering me a beer via this link: https://github.com/sponsors/mangiucugnaMedium4/10/2026
v0.58.7# Fixed Fix the regressions reported in #187: - Fixed a regression where escaped object roots like `{\\"key\\": \\"value\\"}` could be misrepaired as arrays instead of objects. - Improved schema-guided salvage mode so set-like malformed object input such as `{"a", "b"}` can be recovered as `{"a": null, "b": null}` when `schema = {"type": "object"}` is provided and `schema_repair_mode="salvage"` - Added regression coverage around object-vs-array recovery, strict mode, and schema-guided salvagMedium3/26/2026
v0.58.6# Fixed - Typing stubs were all over the place, fixed that and added tests so that mypy does not fail for developers with strict typing # Think about sponsoring this library! This library is free for everyone and it's maintained and developed as a side project so, if you find this library useful for your work, consider offering me a beer via this link: https://github.com/sponsors/mangiucugnaLow3/16/2026
v0.58.5# Fixed - Improved parser performance, especially for common JSON containing ordinary quoted strings. Profiler indicates a 60% improvement in performance. - Hardened schema-guided repair behavior, including safer salvage handling for mixed type unions. # Think about sponsoring this library! This library is free for everyone and it's maintained and developed as a side project so, if you find this library useful for your work, consider offering me a beer via this link: https://github.com/sLow3/7/2026
v0.58.4# Fixed - v0.58.3 added some regressions in which `salvage` mode could repair _less_ than `standard` mode. Fixed that regression and some minor changes. # Think about sponsoring this library! This library is free for everyone and it's maintained and developed as a side project so, if you find this library useful for your work, consider offering me a beer via this link: https://github.com/sponsors/mangiucugnaLow3/5/2026
v0.58.3# Fixed - Fix #181, fix another bug in duplicate-key dedup for array objects # Think about sponsoring this library! This library is free for everyone and it's maintained and developed as a side project so, if you find this library useful for your work, consider offering me a beer via this link: https://github.com/sponsors/mangiucugnaLow3/3/2026
v0.58.2# Fixed - Fix #181, fix a corner in case in which duplicate keys won't be deduplicated and instead split inside an array. # Think about sponsoring this library! This library is free for everyone and it's maintained and developed as a side project so, if you find this library useful for your work, consider offering me a beer via this link: https://github.com/sponsors/mangiucugnaLow3/2/2026
v0.58.1# Fixed - Fix #180, expand schema salvage mode coverage to fix more obvious corner cases that are easily identifiable (unwrapping arrays, add required fields that are missing) # Think about sponsoring this library! This library is free for everyone and it's maintained and developed as a side project so, if you find this library useful for your work, consider offering me a beer via this link: https://github.com/sponsors/mangiucugnaLow2/28/2026
v0.58.0# Added - Fix #178, some additions to the schema based repair mode: - Expand the boolean coercion to more strings that are clearly boolean ("yes"/"no", "y"/"n", etc) - Add a new mode "salvage" that can be passed via "schema_repair_mode". When salvage is passed, the library will try to recover as much information as possible using the schema information and will reduce the number Errors raised. *Pay attention*: this might mean that the returned object is not fully compliant with the scheLow2/17/2026
v0.57.1# Fixed - Valid json that do not validate against the schema are not treated as invalid. For example `{"value":"1"}` is a valid JSON but does not validate against `{"type":"object","properties":{"value":{"type":"integer"}},"required":["value"]}`. With this fix the input is passed to the parser and coerced into the now valid `{"value": 1}`. With this fix the schema validator feature moves to _beta_ status. # Think about sponsoring this library! This library is free for everyone and it's maLow2/8/2026
v0.57.0# Added - Improve schema validation, validate all output produced by the library against the schema including valid json - Fix a corner case found by some automated code review # Think about sponsoring this library! This library is free for everyone and it's maintained and developed as a side project so, if you find this library useful for your work, consider offering me a beer via this link: https://github.com/sponsors/mangiucugnaLow2/7/2026
v0.56.0# Added - Many of you (#177, #170, #87, #52, #50) have asked for the ability of json_repair to follow a jsonschema/Pydantic object. This is the release that adds schema support. Please read README for the instructions. *IMPORTANT* the feature is released as alpha, so keep in mind that the API might change or I might scrap the entire feature if it doesn't work. Any volunteers to test it would be much appreciated! # Think about sponsoring this library! This library is free for everyone and Low2/3/2026
v0.55.2# Fixed - Fix #176, repair missing row arrays in table-like JSON. If a json represents a matrix, change the array-merge heuristic to cover this case. # Think about sponsoring this library! This library is free for everyone and it's maintained and developed as a side project so, if you find this library useful for your work, consider offering me a beer via this link: https://github.com/sponsors/mangiucugnaLow2/2/2026
v0.55.1# Fixed - Fix #174, UUIDs starting with a number and followed by e were incorrectly parsed as scientific notation. # Sponsors This release is sponsored by @Icana-AI and @mjharte. Thank you very much for your generous donation! This library is free for everyone and it's maintained and developed as a side project so, if you find this library useful for your work, consider offering me a beer via this link: https://github.com/sponsors/mangiucugnaLow1/23/2026
v0.55.0# Fixed - Fix #173, manage the edge case in which there are two closing braces for an object inside an array. # Think about sponsoring this library! This library is free for everyone and it's maintained and developed as a side project so, if you find this library useful for your work, consider offering me a beer via this link: https://github.com/sponsors/mangiucugnaLow1/1/2026
v0.54.3# Fixed - Fix #172, in array context, when an object closing brace was missing and it was followed by a closing array bracket, the repair action was incorrect # Think about sponsoring this library! This library is free for everyone and it's maintained and developed as a side project so, if you find this library useful for your work, consider offering me a beer via this link: https://github.com/sponsors/mangiucugnaLow12/15/2025
v0.54.2# Fixed - Fix #170, do not return empty elements in case of multiple elements detected in the same invalid json string # Think about sponsoring this library! This library is free for everyone and it's maintained and developed as a side project so, if you find this library useful for your work, consider offering me a beer via this link: https://github.com/sponsors/mangiucugnaLow11/25/2025
v0.54.1# Fixed - Fix #169, respect python style number notation. # Think about sponsoring this library! This library is free for everyone and it's maintained and developed as a side project so, if you find this library useful for your work, consider offering me a beer via this link: https://github.com/sponsors/mangiucugnaLow11/19/2025
v0.54.0# Added - Fix #162, added a new feature in _beta mode_: `strict=True` will limit the amount of repairs that the parser can do and will return an exception if it attempts to do a repair that is not allowed. Please refer to the docs for an example. # Fixed - Fix #165, sometimes multi-byte unicode characters would be broken when passing a file. # Think about sponsoring this library! This library is free for everyone and it's maintained and developed as a side project so, if you find thisLow11/18/2025
v0.53.1# Fixed - Fix #168, sometimes an empty array was incorrectly parsed when in object context. # Think about sponsoring this library! This library is free for everyone and it's maintained and developed as a side project so, if you find this library useful for your work, consider offering me a beer via this link: https://github.com/sponsors/mangiucugnaLow11/18/2025
v0.53.0# Added Fix #159, sometimes LLMs might insert a ` ```json .. ``` ` block in the middle of a normal json string. Try to manage this edge case and normalize the json. # Think about sponsoring this library! This library is free for everyone and it's maintained and developed as a side project so, if you find this library useful for your work, consider offering me a beer via this link: https://github.com/sponsors/mangiucugnaLow11/8/2025
v0.52.5# Fixed Fixed #160, sometimes very long json could lead to a max recursion depth exception. Fixed now. # Sponsors This release is sponsored by @kojoopuni. Thank you very much for your generous donation! This library is free for everyone and it's maintained and developed as a side project so, if you find this library useful for your work, consider offering me a beer via this link: https://github.com/sponsors/mangiucugnaLow11/6/2025
v0.52.4# Fixed Fixed #158, in some cases the library could return an `IndexError`. Added checks to prevent that. # Think about sponsoring this library! This library is free for everyone and it's maintained and developed as a side project so, if you find this library useful for your work, consider offering me a beer via this link: https://github.com/sponsors/mangiucugnaLow11/1/2025
v0.52.3# Fixed Fixed #156, the parser did not reset context when it detected the potential for multiple jsons in the same string, leading to all kind of possible issues. # Think about sponsoring this library! This library is free for everyone and it's maintained and developed as a side project so, if you find this library useful for your work, consider offering me a beer via this link: https://github.com/sponsors/mangiucugnaLow10/22/2025
v0.52.2# Fixed Fixed #155, handle an edge case in which an object ends without a proper delimiter first and the LLM adds a code fence at the end # Think about sponsoring this library! This library is free for everyone and it's maintained and developed as a side project so, if you find this library useful for your work, consider offering me a beer via this link: https://github.com/sponsors/mangiucugnaLow10/20/2025
v0.52.1# Fixed Fixed #154, sometimes when repairing a confusing delimiter the poor closing brace was involved. Try not do that anymore # Think about sponsoring this library! This library is free for everyone and it's maintained and developed as a side project so, if you find this library useful for your work, consider offering me a beer via this link: https://github.com/sponsors/mangiucugnaLow10/18/2025
v0.52.0# Added - Fixed #153, in some cases you could have `{'element 1', 'element 2'}` in which the square brackets are replaced by curly brackets, added support for this case # Think about sponsoring this library! This library is free for everyone and it's maintained and developed as a side project so, if you find this library useful for your work, consider offering me a beer via this link: https://github.com/sponsors/mangiucugnaLow10/5/2025
v0.51.0# Added - PR #151, from @kba977. Extend an object if the parser detects that it was closed too early. # Sponsors This release is sponsored by @yasyf. Thank you very much for your generous donation! This library is open-source and available for everyone, maintained and improved as a passion project during my free time. If it has made your work easier or added value to your projects, I’d greatly appreciate your support! You can contribute by buying me a virtual beer through this link: Low9/19/2025
v0.50.1# Fixed - Fix #148, strengthen the logic introduced in #146 and prevent an index error. # Sponsors This release is sponsored by @yasyf. Thank you very much for your generous donation! This library is open-source and available for everyone, maintained and improved as a passion project during my free time. If it has made your work easier or added value to your projects, I’d greatly appreciate your support! You can contribute by buying me a virtual beer through this link: [https://githuLow9/6/2025
v0.50.0# Added - Fix #146, the management of wrong delimiters in array context was too simplistic, now it's much more refined # Sponsors This release is sponsored by @yasyf. Thank you very much for your generous donation! This library is open-source and available for everyone, maintained and improved as a passion project during my free time. If it has made your work easier or added value to your projects, I’d greatly appreciate your support! You can contribute by buying me a virtual beer thLow8/20/2025
v0.49.0# Added - PR #144, return empty list as repair action log when `logging=True` but no repair was made. Thanks to @qchappat for contributing! # Fixed - Fix #142: Properly handle escaped characters in skip_to_character. Previously, escaped quotes and backslashes were treated as actual delimiters, breaking JSON parsing for strings with escaped content. # Sponsors This release is sponsored by @yasyf. Thank you very much for your generous donation! This library is open-source and availableLow8/10/2025
v0.48.0# Added - Fix #141, remove superfluous escaping from delimiters when we found one # Sponsors This release is sponsored by @yasyf. Thank you very much for your generous donation! This library is open-source and available for everyone, maintained and improved as a passion project during my free time. If it has made your work easier or added value to your projects, I’d greatly appreciate your support! You can contribute by buying me a virtual beer through this link: [https://github.com/Low7/25/2025
v0.47.8# Fixed - Fix #140, a corner case that happens when an object inside an array has a misplaced quote would mess the object structure # Sponsors This release is sponsored by @yasyf. Thank you very much for your generous donation! This library is open-source and available for everyone, maintained and improved as a passion project during my free time. If it has made your work easier or added value to your projects, I’d greatly appreciate your support! You can contribute by buying me a viLow7/17/2025
v0.47.7# Fixed - Fix #139, a bug array context and stream stable would cause an empty object # Sponsors This release is sponsored by @gitsome and @yasyf. Thank you very much for your generous donation! This library is open-source and available for everyone, maintained and improved as a passion project during my free time. If it has made your work easier or added value to your projects, I’d greatly appreciate your support! You can contribute by buying me a virtual beer through this link: [hLow7/13/2025
v0.47.6# Fixed - Fix #138, fix a regression on handling dangling quotes # Sponsors This release is sponsored by @yasyf. Thank you very much for your generous donation! This library is open-source and available for everyone, maintained and improved as a passion project during my free time. If it has made your work easier or added value to your projects, I’d greatly appreciate your support! You can contribute by buying me a virtual beer through this link: [https://github.com/sponsors/mangiucuLow7/1/2025
v0.47.5# Fixed - Fix #137, comments were parsed wrongly if at the beginning of a string # Sponsors This release is sponsored by @yasyf. Thank you very much for your generous donation! This library is open-source and available for everyone, maintained and improved as a passion project during my free time. If it has made your work easier or added value to your projects, I’d greatly appreciate your support! You can contribute by buying me a virtual beer through this link: [https://github.com/sLow6/30/2025
v0.47.4# Fixed - Fix #135, fix a crash due to a regression # Sponsors This release is sponsored by @yasyf. Thank you very much for your generous donation! This library is open-source and available for everyone, maintained and improved as a passion project during my free time. If it has made your work easier or added value to your projects, I’d greatly appreciate your support! You can contribute by buying me a virtual beer through this link: [https://github.com/sponsors/mangiucugna](https://Low6/27/2025
v0.47.3# Fixed - Add a fix for a corner case when the string is heavily escaped # Sponsors This release is sponsored by @yasyf. Thank you very much for your generous donation! This library is open-source and available for everyone, maintained and improved as a passion project during my free time. If it has made your work easier or added value to your projects, I’d greatly appreciate your support! You can contribute by buying me a virtual beer through this link: [https://github.com/sponsors/Low6/24/2025
v0.47.2# Fixed - Fix #134, when `"key": ,` is found, fill the value with an empty string # Sponsors This release is sponsored by @yasyf. Thank you very much for your generous donation! This library is open-source and available for everyone, maintained and improved as a passion project during my free time. If it has made your work easier or added value to your projects, I’d greatly appreciate your support! You can contribute by buying me a virtual beer through this link: [https://github.com/Low6/23/2025
v0.47.1# Minor - You can now pass to `repair_json` all extra arguments that `json.dumps` accepts, no need to do workarounds anymore! # Sponsors This release is sponsored by @yasyf. Thank you very much for your generous donation! This library is open-source and available for everyone, maintained and improved as a passion project during my free time. If it has made your work easier or added value to your projects, I’d greatly appreciate your support! You can contribute by buying me a virtuaLow6/19/2025
v0.47.0# Added - Fix #132, support more complex escape sequences especially when many escapes are spit by the LLM # Sponsors This release is sponsored by @yasyf. Thank you very much for your generous donation! This library is open-source and available for everyone, maintained and improved as a passion project during my free time. If it has made your work easier or added value to your projects, I’d greatly appreciate your support! You can contribute by buying me a virtual beer through thisLow6/19/2025
v0.46.2# Fixed - Fix #131, respect the unicode escape sequences to behave like `json` after a repair action # Sponsors This release is sponsored by @yasyf. Thank you very much for your generous donation! This library is open-source and available for everyone, maintained and improved as a passion project during my free time. If it has made your work easier or added value to your projects, I’d greatly appreciate your support! You can contribute by buying me a virtual beer through this link:Low6/6/2025
v0.46.1# Fixed - Fix #130, an edge case would send the parser in an infinite loop # Sponsors This release is sponsored by @yasyf. Thank you very much for your generous donation! This library is open-source and available for everyone, maintained and improved as a passion project during my free time. If it has made your work easier or added value to your projects, I’d greatly appreciate your support! You can contribute by buying me a virtual beer through this link: [https://github.com/sponsLow6/4/2025
v0.46.0# Added - Fix #126, add a fix for an edge case in which an unclosed array inside an object leads to a weird parsing mistake # Sponsors This release is sponsored by @yasyf. Thank you very much for your generous donation! This library is open-source and available for everyone, maintained and improved as a passion project during my free time. If it has made your work easier or added value to your projects, I’d greatly appreciate your support! You can contribute by buying me a virtual Low5/22/2025
v0.45.1# Fixed - Fixed #123, a regression when added the ability to parse comments - [minor] Indicate correctly in pyproject.toml that now we support only >=3.10 # Sponsors This release is sponsored by @yasyf. Thank you very much for your generous donation! This library is open-source and available for everyone, maintained and improved as a passion project during my free time. If it has made your work easier or added value to your projects, I’d greatly appreciate your support! You can contLow5/21/2025
v0.45.0# **Note that this version was yanked from pypi due to an error in pyproject.toml, please use 0.45.1 for python >= 3.10** # Added - Fix #121, in `repair_json()` skip `json.dumps()` if the parser returns an empty string. This breaks the old behavior, so make sure to adjust your logic if you rely on this # Fixed - Drop support for 3.9, since it has been deprecated for over 6 months # Sponsors This release is sponsored by @yasyf. Thank you very much for your generous donation! This lLow5/20/2025

Dependencies & License Audit

Loading dependencies...

Similar Packages

partial-json-parserParse partial JSON generated by LLM0.2.1.1.post7
pyhoconHOCON parser for Python0.3.63
langsmithClient library to connect to the LangSmith Observability and Evaluation Platform.0.7.33
azure-coreMicrosoft Azure Core Library for Pythonazure-template_0.1.0b6187637
azure-mgmt-coreMicrosoft Azure Management Core Library for Pythonazure-template_0.1.0b6187637