A zero-dependency Go library designed to automatically detect and repair malformed or broken JSON data generated by Large Language Models (LLMs), supporting both API and CLI usage.
Overview#
json-repair is a Go library specifically designed to address JSON formatting anomalies in LLM outputs. When using Large Language Models for Function Calling or text generation, the JSON output often contains formatting errors that cause standard JSON parsers to fail. json-repair automatically fixes these issues, improving system robustness.
Core Capabilities#
Supported JSON Repairs#
- Single quote strings → Auto-convert to double quotes
- Mixed quote strings
- Unclosed arrays/objects → Auto-complete
],} - Malformed literals (TRUE, FALSE, Null) → Fix to correct casing
- JSON comments (
//or/* */) → Auto-remove - Newline characters
\nin strings - Markdown code block markers (```json)
- Incorrect key-value pairs
- Strings containing URLs
Technical Features#
- Zero Dependencies: Uses only Go standard library
- Robust Design: Always returns string result, never panics on errors
- Recursion Protection: Maximum depth limit (1000 levels) to prevent stack overflow
- Infinite Loop Detection: Progress counter detects infinite loops
- Panic Recovery: Uses defer/recover to catch exceptions
Installation & Usage#
Go Library#
go get github.com/RealAlexandreAI/json-repair
CLI Tool#
brew install realalexandreai/tap-jsonrepair/jsonrepair
API Example#
package main
import (
"github.com/RealAlexandreAI/json-repair"
)
func main() {
brokenJSON := "```json {'employees':['John', 'Anna', ```"
fixedJSON, _ := jsonrepair.RepairJSON(brokenJSON)
// Output: {"employees":["John","Anna"]}
}
Core APIs#
RepairJSON(src string) (dst string, err error)- Standard repair interfaceMustRepairJSON(src string) (dst string)- Simplified interface, no error returnJSONMarshal(t any) ([]byte, error)- Enhanced serialization, preserves HTML characters
CLI Usage#
jsonrepair -i "{'test': 'value',"
jsonrepair -f broken.json
cat broken.json | jsonrepair
Use Cases#
- LLM Application Post-processing: Clean JSON data returned by ChatGPT, Claude, etc.
- Agent Workflows: Ensure JSON message validity in multi-agent systems
- Function Calling: Handle function call return data from OpenAI and similar platforms
- Data Cleaning ETL: Repair stored corrupted JSON logs or data files