jsonrepair-rs
Repair broken JSON in Rust. Fix quotes, commas, comments, trailing content, and 30+ other issues commonly found in LLM outputs.
Rust port of josdejong/jsonrepair.
Features
| Category | Examples |
|---|---|
| Quote repair | Single quotes → double, curly quotes, backticks, unquoted keys |
| Comma repair | Missing, trailing, and leading commas |
| Comments | //, /* */, # — stripped from output |
| Python keywords | True → true, False → false, None → null |
| JS keywords | undefined → null, NaN → null, Infinity → null |
| Markdown fences | ```json ... ``` — extracted and repaired |
| Truncated JSON | Auto-closes unclosed brackets, braces, and strings |
| Number repair | Leading zeros, trailing dots (2. → 2.0), truncated exponents |
| String repair | Concatenation ("a" + "b"), invalid escapes, unescaped control chars |
| JSONP | callback({...}) → {...} |
| MongoDB | ObjectId("...") → "...", NumberLong("...") → "..." |
| NDJSON | Newline-delimited JSON → JSON array |
| Ellipsis | [1, 2, ...] → [1, 2] |
| Misc | BOM stripping, special whitespace, trailing semicolons |
Usage
Add to your Cargo.toml:
[]
= "0.1"
use jsonrepair;
// Fix single quotes (whitespace preserved)
let result = jsonrepair.unwrap;
assert_eq!;
// Fix trailing commas
let result = jsonrepair.unwrap;
assert_eq!;
// Strip markdown fences
let result = jsonrepair.unwrap;
assert_eq!;
// Convert Python keywords
let result = jsonrepair.unwrap;
assert_eq!;
// Handle LLM output with comments
let result = jsonrepair.unwrap;
assert_eq!;
Error handling
use ;
match jsonrepair
Acknowledgments
This is a Rust port of jsonrepair by Jos de Jong.