Visit Sinki.ai for Enterprise Databricks Services | Simplify Your Data Journey
Jellyfish Technologies Logo

Repairing Broken JSON in Python with json-repair

Repairing Broken JSON in Python with json-repair

Working with JSON in Python is usually smooth until it’s not. If you’ve ever dealt with malformed JSON strings from APIs, logs, web scraping, or LLM outputs, you know how frustrating it is when json.loads() throws a JSONDecodeError.

Enter json-repair, a powerful new Python package designed to automatically fix broken JSON so you can work with it effortlessly.

What is json-repair?

Json-repair iis a Python library that auto-fixes invalid or malformed JSON strings. It corrects common issues such as:

  • Missing or unquoted property names
  • Single quotes instead of double quotes
  • Python-style booleans (True, False, None)
  • Trailing commas
  • Missing or mismatched brackets

This makes it an excellent choice for post-processing JSON returned by large language models, user input, or third-party services.

Installation

Install via pip:

pip install json-repair

Basic Usage

from json_repair import repair_json

broken = """{ name: 'Alice', age:30, hobbies:['reading', 'gaming' }"""
fixed = repair_json(broken)
print(fixed)

Output:

{
  "name": "Alice",
  "age": 30,
  "hobbies": ["reading", "gaming"]
}

Now you can parse it with Python’s built-in json module:

import json
data = json.loads(fixed)
print(data['name'])  # Alice

Advanced Usage with loads

If you want to skip the intermediate string repair and load directly:

import json
data = json.loads(fixed)
print(data['name'])  # Alice

Output:

{'user': 'John', 'active': True, 'skills': ['python', 'ai']}

Why Not Just Regex?

While you might consider fixing JSON errors with regex, it’s error-prone and unreliable. JSON is hierarchical, and regex can’t understand nested structures or contextual issues. json-repair uses a parser-based approach, making it robust and safe.

Alternative: fix-busted-json

Another package worth mentioning is fix-busted-json. It handles similar issues and provides utilities to extract and repair multiple JSON objects from a messy string:

from fix_busted_json import repair_json

broken = "{ name: 'John' 'age': 30, }"
print(repair_json(broken))

Output:

{
  "name": "John",
  "age": 30
}

When Should You Use json-repair?

  • Fixing LLM-generated JSON responses
  • Cleaning up scraped data or embedded JSON from HTML
  • Preprocessing logs with broken JSON fragments
  • Restoring corrupted config files or API outputs

Final Thoughts

If you’re dealing with unreliable JSON and tired of writing defensive code or custom parsers, give json-repair a try. It’s lightweight, effective, and will save you hours of debugging.

Summary:

  • Install with pip install json-repair
  • Use repair_json() to sanitize broken JSON strings
  • Use json_repair.loads() to directly parse into Python objects
  • For log-heavy workflows, consider fix-busted-json

Don’t let broken JSON break your flow. Repair it and move on!

Share this article
Want to speak with our solution experts?
Jellyfish Technologies

Modernize Legacy System With AI: A Strategy for CEOs

Download the eBook and get insights on CEOs growth strategy

    Let's Talk

    We believe in solving complex business challenges of the converging world, by using cutting-edge technologies.