jsonrepair-rs
Repair broken JSON in Rust.
jsonrepair-rs takes malformed JSON-like text (often from LLM output) and returns valid JSON text.
Rust port of josdejong/jsonrepair.
Installation
Add this to your Cargo.toml:
[]
= "0.1"
Quick Start
This crate is a library crate (no built-in CLI binary).
use jsonrepair;
Output:
Examples
This repository includes runnable examples in examples/:
repair_basic: repairs a malformed JSON-like string and prints the result.repair_and_parse: repairs input, parses withserde_json, and validates key fields.
API
- Input: malformed JSON-like text.
- Output: repaired JSON string.
- On failure: returns
JsonRepairErrorwithkind,position,line, andcolumn.
If you need a typed value, parse the repaired string with serde_json:
use jsonrepair;
let repaired = jsonrepair.unwrap;
let value: Value = from_str.unwrap;
assert_eq!;
Common Repairs
| Category | Examples |
|---|---|
| Quote repair | single quotes, curly quotes, backticks, unquoted keys |
| Comma repair | missing commas, trailing commas, leading commas |
| Comments | //, /* */, # comments are removed |
| Python keywords | True → true, False → false, None → null |
| JS keywords | undefined/NaN/Infinity → null |
| Markdown fences | extracts content from fenced blocks like json ... |
| Truncated JSON | auto-closes missing ], }, and string terminators |
| Number fixes | leading zeros, trailing dots (2. → 2.0), truncated exponents |
| String fixes | concatenation ("a" + "b"), invalid escapes, control chars |
| JSONP | callback({...}) → {...} |
| MongoDB wrappers | ObjectId("..."), NumberLong("...") |
| NDJSON | newline-delimited JSON converted to a JSON array |
| Ellipsis | [1, 2, ...] → [1, 2] |
Error Handling
use ;
match jsonrepair
Notes
- Maximum supported nesting depth is 512.
- When no safe repair is possible, the function returns an error instead of guessing.
Development
# Build and test
# CI-equivalent checks
RUSTFLAGS="-Dwarnings"
Pre-commit
# Install pre-commit (using uv)
# Install git hook for this repo
# Run hooks manually
Benchmarks
Acknowledgments
This is a Rust port of jsonrepair by Jos de Jong.