compare-json (Rust)
compare-json is the Rust port of compare-json — structured JSON comparison with the same semantics as the engine that powers this website. It detects additions, deletions, type changes, and value changes between two JSON values, with control over how keys, values, and arrays are matched. Ships as both a library and a CLI, with full serde integration.
Language: Rust — distributed via crates.io. Source code on GitHub.
Installation
# CLI
cargo install compare-json
Or as a library:
[dependencies]
compare-json = "0.1"
Quick Start
Library
use compare_json::{compare_json, ArrayCompareMethod, CompareOptions};
use serde_json::json;
let base = json!({"name": "Alice", "age": 30});
let contrast = json!({"name": "Bob", "age": "30", "email": "[email protected]"});
let options = CompareOptions::default(); // byIndex arrays, all flags off
let diffs = compare_json(&base, &contrast, &options);
for d in &diffs {
println!("{} {} {}", d.path_string, d.path_belongs_to, d.diff_type);
}
// name both valueChanged
// age both typeChanged
// email contrast added
// With options
let options = CompareOptions {
array_compare_method: Some(ArrayCompareMethod::Lcs), // or ByIndex, Unordered
key_case_insensitive: true,
value_case_insensitive: false,
numeric_string_equals_number: true,
};
CLI
# Compare JSON files
compare-json base.json contrast.json
# Array strategies and case-insensitive matching
compare-json base.json contrast.json -a lcs -k -v
# Machine-readable JSON output
compare-json base.json contrast.json --json-export
# Write the report to a file
compare-json base.json contrast.json -o diff.txt
Options
| Flag | Description |
|---|---|
-a, --array-compare-method <method> | Array comparison strategy: byIndex (default), lcs, unordered |
-k, --key-case-insensitive | Compare object keys case-insensitively |
-v, --value-case-insensitive | Compare string values case-insensitively |
--numeric-string-equals-number | Treat numeric strings as equal to numbers |
-j, --json-export | Output the differences as JSON |
-o, --output <file> | Write output to a file instead of stdout |
See Comparison Options and Array Comparison Methods for what each setting does.
License
MIT