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

FlagDescription
-a, --array-compare-method <method>Array comparison strategy: byIndex (default), lcs, unordered
-k, --key-case-insensitiveCompare object keys case-insensitively
-v, --value-case-insensitiveCompare string values case-insensitively
--numeric-string-equals-numberTreat numeric strings as equal to numbers
-j, --json-exportOutput 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