RealAlexandreAI/json-repair

🔧 Repair JSON!Solution for JSON Anomalies from LLMs.

What it solves

Large language models often produce JSON that is not valid: missing closing brackets, unquoted strings, trailing commas, comments, code fences, and even non-ASCII punctuation. json-repair takes that messy output and turns it into compact, valid JSON so it can be parsed reliably.

How it works

The library first normalizes the input: it strips code fences (like ```json), comments (//, /* */, #), and converts full-width punctuation to ASCII. If the input is already valid JSON, it is simply compacted. Otherwise, a repair parser runs to fix structural issues. The parser can be configured with options like strict mode (rejecting duplicate keys) and schema-guided repair, which uses a JSON Schema to steer fixes, including a "salvage" mode that drops invalid array items. The output is always a compact JSON string.

Who it’s for

Developers who need to handle imperfect JSON output from LLMs in Go applications or from the command line. It’s especially useful in pipelines where parsing failures would otherwise break automation.

Highlights

  • Pure Go library with no third-party dependencies (Go 1.21)
  • Simple functions: RepairJSON, MustRepairJSON, RepairValue, and more
  • Handles a wide range of malformations: missing commas, unclosed strings, trailing commas, duplicate keys, comments, and full-width characters
  • Optional JSON Schema support to guide repairs, including a salvage mode that drops invalid array items
  • Includes a small CLI (jsonrepair) with flags for inline input and files

Related

  • Project
  • Project
  • Project
  • Project
  • Project