A powerful and flexible JSON diff library for Rust that helps you compare two JSON values and identify differences.
- Comprehensive Diff Detection: Detects added, removed, and modified fields in JSON objects
- Array Comparison: Compare arrays with optional order-insensitive matching
- Nested Structure Support: Handles deeply nested JSON objects and arrays
- Multiple Output Formats: Format diffs as plain text, JSON, or compact summaries
- Easy-to-Use API: Simple and intuitive API for quick integration
- Zero Dependencies: Minimal external dependencies (only serde and serde_json)
Add this to your Cargo.toml:
[dependencies]
diff_json = "0.1.0"use diff_json::{compare_json, DiffFormatter};
let json1 = r#"{"name": "Alice", "age": 30}"#;
let json2 = r#"{"name": "Alice", "age": 31}"#;
let diffs = compare_json(json1, json2).unwrap();
let formatter = DiffFormatter::new();
println!("{}", formatter.format(&diffs));use diff_json::{compare_values, JsonDiff};
use serde_json::json;
let v1 = json!({"name": "Alice", "age": 30});
let v2 = json!({"name": "Bob", "age": 31});
let diffs = compare_values(&v1, &v2);
for diff in diffs {
println!("{}", diff);
}use diff_json::JsonDiff;
use serde_json::json;
let v1 = json!([1, 2, 3]);
let v2 = json!([3, 2, 1]);
let differ = JsonDiff::new().ignore_order(true);
let diffs = differ.diff(&v1, &v2);
// diffs will be empty since arrays contain the same elementsuse diff_json::{compare_values, DiffFormatter};
use serde_json::json;
let v1 = json!({"name": "Alice"});
let v2 = json!({"name": "Bob"});
let diffs = compare_values(&v1, &v2);
let formatter = DiffFormatter::new();
// Plain text format
println!("{}", formatter.format(&diffs));
// Compact format
println!("{}", formatter.format_compact(&diffs));
// JSON format
println!("{}", formatter.format_json(&diffs));Compare two JSON strings and return a list of differences.
Compare two serde_json::Value instances and return a list of differences.
Main diff engine with configurable options.
new(): Create a newJsonDiffinstanceignore_order(bool): Set whether array comparison should ignore orderdiff(v1: &Value, v2: &Value) -> Vec<Diff>: Compare two values
Format diff results in various styles.
new(): Create a new formatterindent(&str): Set indentation for formatted outputshow_values(bool): Control whether to show values in outputformat(&[Diff]) -> String: Format diffs as plain textformat_compact(&[Diff]) -> String: Format diffs in compact styleformat_json(&[Diff]) -> String: Format diffs as JSONformat_colored(&[Diff]) -> String: Format diffs with color indicators
Represents a single difference between two JSON values.
path: String: JSON path to the changed elementdiff_type: DiffType: Type of change (Added, Removed, Modified, Moved)old_value: Option<Value>: Original value (if applicable)new_value: Option<Value>: New value (if applicable)
This project is licensed under either of:
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
at your option.
Contributions are welcome! Please feel free to submit a Pull Request.
See the examples directory for more usage examples.
Run tests with:
cargo testMIT OR Apache-2.0