json_repair

Repair malformed JSON from LLMs, APIs, logs, and user input in Python.

What is json_repair?

Repair malformed JSON from LLMs, APIs, logs, and user input in Python.

Overview of json_repair

What it is

json_repair is a Python library that repairs malformed JSON strings from LLMs, APIs, logs, and user input. It fixes missing quotes, commas, brackets, comments, stray prose, and truncated values. It can be used as a drop-in fallback for json.loads() or as a schema-guided repair step.

Key features

  • Fixes missing quotes, misplaced commas, unescaped characters, and incomplete key-value pairs.
  • Repairs broken arrays and objects by adding necessary elements (e.g., commas, brackets) or default values (null, empty string).
  • Auto-completes missing JSON values with reasonable defaults (like empty strings or null).
  • Recognizes Python-style tuples: comma-separated parenthesized sequences become JSON arrays, while a single parenthesized value remains a scalar.
  • Recognizes true/false/null and None case-insensitively as JSON booleans or null within arrays, objects, and tuples.
  • Provides loads() and load() as drop-in replacements for json.loads() and json.load(), with a from_file() method for reading from a file.
  • Supports skip_json_loads=True to bypass the initial strict JSON validation and go directly to the repair parser.

Use cases

  • Fixing malformed JSON output from LLMs that may skip parentheses or add extra words.
  • Repairing JSON from APIs, logs, or user input that contains syntax errors like missing quotes, misplaced commas, or unescaped characters.
  • Auto-completing missing JSON values in incomplete data structures.
  • Cleaning up JSON that includes extra non-JSON characters like comments or improperly placed characters.

Getting started

pip install json-repairThen use in code:pythonfrom json_repair import repair_jsongood_json_string = repair_json(bad_json_string)Or as a drop-in replacement for json.loads():pythonimport json_repairdecoded_object = json_repair.loads(json_string)

License and pricing

This library is open source under the MIT license. It is free for everyone.

Based on the project's public README and repository metadata.

Add this badge to your README

Featured on YunoDB Badge styles & help

Maintain this project? Show that it is featured on YunoDB.

Markdown[![Featured on YunoDB](https://yunodb.com/badge/mangiucugnajson-repair.svg)](https://yunodb.com/tools/mangiucugnajson-repair?utm_source=github&utm_medium=badge)
HTML<a href="https://yunodb.com/tools/mangiucugnajson-repair?utm_source=github&utm_medium=badge"><img src="https://yunodb.com/badge/mangiucugnajson-repair.svg" alt="Featured on YunoDB" height="20"></a>