JSON Repair

0.58.7 · active · verified Mon Apr 06

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.

Warnings

Install

Imports

Quickstart

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.

from json_repair import repair_json, loads
import json

broken_json_string = """{ 
  'name': 'Alice', 
  age: 30, 
  'isStudent': True,
  'hobbies': ['reading', 'gaming',],
}"""

# Using repair_json to get a fixed JSON string
repaired_string = repair_json(broken_json_string)
print(f"Repaired string: {repaired_string}")
parsed_data = json.loads(repaired_string)
print(f"Parsed data (using json.loads): {parsed_data}")

# Using loads for direct parsing to Python object
# This method already includes the repair logic internally
parsed_data_direct = loads(broken_json_string)
print(f"Parsed data (using json_repair.loads): {parsed_data_direct}")

view raw JSON →