Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
jsonrepair
A fast, low-dependency JSON repair library for Rust. Fixes malformed JSON commonly produced by LLMs and other sources.
Features
- Non-streaming API: Simple string-to-string repair
- Streaming API: Process large files or chunked input with low memory usage
- Writer API: Stream output while parsing to minimize memory overhead
- Parse to Value: Direct conversion to
serde_json::Value(like Python'sjson.loads()) - CLI tool: Command-line interface (
jsonrepair/jr) - Language bindings: Python (PyO3), C API for C/C++/Go/Java/C#/Node.js/etc.
Installation
[]
= "0.1"
Quick Start
Basic Repair
use ;
let broken = "{name: 'John', age: 30,}";
let fixed = repair_json?;
// Result: {"name":"John","age":30}
Parse to Value
use loads;
let value = loads?;
assert_eq!;
Streaming (Large Files)
use StreamRepairer;
let mut repairer = new;
for chunk in chunks
if let Some = repairer.flush?
Load from File
use from_file;
let value = from_file?;
What It Fixes
- Comments:
//,/* ... */,#(optional) - Quotes: Single quotes → double quotes, unquoted keys/strings
- Punctuation: Missing commas/colons, trailing commas, unclosed brackets
- Wrappers: Fenced code blocks (
json ...), JSONP (callback(...)) - String concatenation:
"a" + "b"→"ab" - Regex literals:
/pattern/→"/pattern/" - Keywords: Python
True/False/None, JavaScriptundefined - Numbers:
NaN/Infinity→null, leading zeros handling - NDJSON: Multiple values → array (optional aggregation)
API Reference
Non-Streaming
// Repair to string
repair_json // Parse to Value (requires 'serde' feature)
loads // Write to writer
repair_to_writer
Streaming
let mut repairer = new;
repairer.push .flush // Writer variants
repairer.push_to_writer
Options
Options
CLI Usage
Install:
Basic usage:
# From file
# From stdin
|
# In-place edit
# Streaming mode (low memory)
# Pretty print
Options:
-o, --output FILE Output file (default: stdout)
--in-place Overwrite input file
--stream Streaming mode (low memory)
--ndjson-aggregate Aggregate NDJSON into array
--pretty Pretty-print output
--ensure-ascii Escape non-ASCII characters
--no-python-keywords Disable Python keyword normalization
--no-undefined-null Disable undefined → null
--no-fence Disable fence stripping
--no-hash-comments Disable # comments
Language Bindings
Python
Native Python bindings using PyO3:
# Repair and parse
=
# {'name': 'John', 'age': 30}
# Just repair, return string
=
# '{"a":1}'
# Load from file
=
# Streaming API
=
See python/ for complete documentation and examples.
C/C++
Build with C API:
This generates:
- Header:
include/jsonrepair.h - Library:
target/release/libjsonrepair.{so,dylib,dll}
Example:
char* fixed = ;
; // {"a":1}
;
See examples/c_example for complete examples.
Go
// #cgo LDFLAGS: -L./target/release -ljsonrepair
// #include "include/jsonrepair.h"
import "C"
result := C.jsonrepair_repair(C.CString("{a:1}"))
defer C.jsonrepair_free(result)
fmt.Println(C.GoString(result))
See examples/go_example for complete examples.
Other Languages
The C API works with any language supporting C FFI:
- Java: JNA, JNI
- C#: P/Invoke
- Node.js: node-ffi, napi
- Ruby: FFI gem
Performance
This library uses a zero-copy architecture (hand-written recursive descent parser with &str slicing) optimized for:
- Medium to large JSON (1KB - 1MB+)
- API responses, database exports, config files
- Batch processing, file parsing
For NDJSON streams or heavily commented small objects, consider llm_json which uses a different architecture optimized for those patterns.
Run benchmarks:
Related Projects
- json_repair (Python) - The original Python implementation that inspired this project
- llm_json (Rust) - Alternative Rust implementation, also based on json_repair
- jsonrepair (TypeScript) - TypeScript implementation by Jos de Jong
Acknowledgments
This project is inspired by and aims to be compatible with json_repair by Stefano Baccianella. We're grateful for the excellent design and comprehensive test suite that made this implementation possible.
License
MIT or Apache-2.0