Skip to main content

json-repair-engine

CI PyPI version Python

A JSON repair engine built for LLM-generated output. Fixes common syntax errors and produces a structured diagnostic report for every repair.

Problem

LLMs frequently emit malformed JSON — trailing commas, Python-style literals (True, False, None), unquoted keys, unterminated structures, comments, single-quoted strings, and more. Standard JSON parsers reject these outright, forcing brittle regex workarounds in every integration.

Most repair tools fix silently. json-repair-engine explains every change.

Features

  • Python literals — True → true, False → false, None → null, undefined → null
  • Trailing commas — strips commas before } and ]
  • Unquoted keys — quotes bare identifiers in objects ({key: "val"} → {"key": "val"})
  • Single-quoted strings — converts 'string' to "string"
  • Comments — strips //, /* */, and # comments
  • Missing commas — inserts commas between adjacent values ([1 2 3] → [1, 2, 3])
  • Extra commas — collapses doubled commas ([1,,2] → [1,2])
  • Unterminated structures — closes missing braces, brackets, and strings
  • Trailing garbage — extracts valid JSON from text with extra content before/after
  • Markdown fences — extracts JSON from ```json ... ``` blocks
  • Hex literals — 0xFF → 255
  • String-aware repair — all replacements skip quoted string contents
  • Structured diagnostics — every repair includes type, position, original, replacement, and message
  • Confidence scoring — each fix has a penalty; aggregate score in the report
  • Zero dependencies — standard library only

Installation

pip install json-repair-engine

Usage

Python API

from jsonrepair import loads

data, report = loads('{"ok": True, "items": [1, 2,],}')

print(data)              # {'ok': True, 'items': [1, 2]}
print(report.valid)      # True
print(report.repaired)   # True
print(report.confidence) # e.g. 0.97
print(report.repair_count)  # 3

for fix in report.fixes:
    print(f"[{fix.type}] at position {fix.position}: {fix.message}")

CLI

$ jsonrepair response.json
{
  "ok": true,
  "items": [1, 2]
}

--- Repair Report ---
{
  "valid": true,
  "repaired": true,
  "confidence": 0.97,
  "repairs": 3
}

API Reference

loads(text: str) -> tuple[dict | list, RepairReport]

Parse and repair a JSON string. Returns the parsed data (object or array) and a repair report.

RepairReport

Field Type Description
valid bool Whether the result is valid JSON
repaired bool Whether any repairs were applied
confidence float Aggregate confidence (starts at 1.0, penalized per fix)
fixes list[Fix] List of individual repairs applied
repair_count int Total number of fixes

Fix

Field Type Description
type str Category of the fix
position int Character offset where the fix was applied
original str Original malformed text
replacement str Corrected text
message str Human-readable description

Fix types

Type Penalty Example
TrailingComma 0.01 [1, 2,] → [1, 2]
MarkdownFence 0.01 ```json ... ``` → inner JSON
CommentStripped 0.01 // comment → removed
InvalidLiteral 0.02 True → true
UndefinedLiteral 0.02 undefined → null
UnquotedKey 0.03 {key: "v"} → {"key": "v"}
HexLiteral 0.03 0xFF → 255
SingleQuoteString 0.05 'val' → "val"
ExtraComma 0.05 [1,,2] → [1,2]
TrailingGarbage 0.05 {"a":1} extra → {"a":1}
MissingComma 0.08 [1 2] → [1, 2]
StructuralClosure 0.15 {"a":1 → {"a":1}

License

MIT

Metadata

Release files for json-repair-engine 0.1.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for json-repair-engine 0.1.7
File Size Uploaded
json_repair_engine-0.1.7.tar.gz 12.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for json-repair-engine 0.1.7
File Interpreter ABI Platform
json_repair_engine-0.1.7-py3-none-any.whl Python 3 none any Details

Total release size: 22.1 kB

Release files / json_repair_engine-0.1.7.tar.gz

Download URL json_repair_engine-0.1.7.tar.gz
Size 12.2 kB
Tags Source
SHA-256 checksum
How to use checksums
53e94de2236bee7699f722e235aa5f6cf1a0be305c2487a18f0a4997e4ce9dee
BLAKE2b-256 checksum
How to use checksums
e92e063a33adf1f0ac096fb25388015de805c894baa07d034a75f766f22726d9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release files / json_repair_engine-0.1.7-py3-none-any.whl

Download URL json_repair_engine-0.1.7-py3-none-any.whl
Size 9.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c38a9bc00a6eb6c011f340c26afcf9272f39ad619479a9f72328e49ec59d74b9
BLAKE2b-256 checksum
How to use checksums
cf002bfaae28bbd7024bdf05877c8932d47d9e23ed7234c1ba26e79bf6df8504
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release history Release notifications | RSS feed

This release

0.1.7 This release

2 release files

0.1.6

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page