Skip to content

Latest commit

 

History

History
131 lines (91 loc) · 2.95 KB

File metadata and controls

131 lines (91 loc) · 2.95 KB

json-diff

Deep JSON comparison, patching, and merge tool for the terminal. Zero external dependencies.

License: MIT Python 3.7+

Features

  • Deep diff two JSON files (added/removed/changed keys)
  • Nested objects and arrays support
  • Colored terminal output
  • Output as RFC 6902 JSON Patch
  • Apply patches to JSON documents
  • Ignore specific paths during comparison
  • Sort keys before comparing
  • Diff arrays by ID field (match items by key instead of index)
  • Three-way merge with conflict detection
  • Summarize changes (count by type)
  • Compare API responses
  • Pipe-compatible (reads from stdin with -)

Installation

# Clone and use directly - no pip install needed
git clone https://github.com/Anuar-boop/json-diff.git
cd json-diff

# Make executable
chmod +x json_diff.py

# Optional: add to PATH
ln -s $(pwd)/json_diff.py /usr/local/bin/json-diff

Usage

Compare two JSON files

# Colored terminal output
python3 json_diff.py diff file1.json file2.json

# Output as RFC 6902 JSON Patch
python3 json_diff.py diff file1.json file2.json --patch

# Output as raw JSON
python3 json_diff.py diff file1.json file2.json --json

# Summary only (counts)
python3 json_diff.py diff file1.json file2.json --summary

Ignore specific paths

python3 json_diff.py diff file1.json file2.json --ignore /metadata/timestamp /version

Sort keys before comparing

python3 json_diff.py diff file1.json file2.json --sort-keys

Diff arrays by ID field

When arrays contain objects with a unique ID, match by that field instead of array index:

python3 json_diff.py diff users1.json users2.json --array-id id

Apply a patch

# Generate a patch
python3 json_diff.py diff old.json new.json --patch > patch.json

# Apply it
python3 json_diff.py apply original.json patch.json

# Apply and save to file
python3 json_diff.py apply original.json patch.json -o patched.json

Three-way merge

python3 json_diff.py merge base.json ours.json theirs.json

# Save merged result
python3 json_diff.py merge base.json ours.json theirs.json -o merged.json

Conflicts are reported on stderr. Non-conflicting changes from both sides are merged automatically.

Compare API responses

python3 json_diff.py compare-api response1.json response2.json

# Ignore volatile fields
python3 json_diff.py compare-api resp1.json resp2.json --ignore /timestamp /request_id

Pipe from stdin

curl -s https://api.example.com/v1/data | python3 json_diff.py diff - local.json

Output Example

+ /users/2: {"name": "Charlie", "role": "admin"}
- /config/debug: true
~ /version:
  - "1.0.0"
  + "2.0.0"

Requirements

  • Python 3.7+
  • No external dependencies (uses json and argparse from stdlib)

License

MIT