Skip to content
jsoncsvonline

Come confrontare due file JSON e trovare le differenze

Pubblicato il

Impara a confrontare due file JSON online o con Python e JavaScript. Individua aggiunte, rimozioni e modifiche senza inviare dati a server.

Quick answer

To compare two JSON files, use a structural JSON diff rather than a flat line-by-line text comparison. A structural diff parses both documents into data objects, ignoring cosmetic differences like indentation and object key reordering (as specified by RFC 8259), while highlighting genuine added, removed, and changed values. You can compare JSON files instantly and privately on your device using our free online JSON Diff tool, or programmatically using DeepDiff in Python, fast-json-patch in Node.js, or jq with diff in your terminal.


Why standard text diffs fail on JSON

When developers use standard tools like Unix diff, Git diff, or text editor comparators on JSON files, they frequently run into false positives:

  1. Object key reordering: In JSON, {"name": "Alice", "role": "admin"} and {"role": "admin", "name": "Alice"} represent identical data. A line diff reports both lines as modified; a structural JSON diff marks them identical.
  2. Indentation and formatting: Comparing 2-space indented JSON against 4-space or minified JSON triggers differences on every line in a text diff, obscuring the actual data changes.
  3. Array index shifts: If an item is added to the beginning of a list, a line diff highlights the entire rest of the array as altered rather than identifying the single inserted item.

Method 1: Compare JSON files online (free & private)

For visual inspection without installing software:

  1. Open the JSON Diff & Compare Tool.
  2. Drag and drop your baseline file into the Original JSON panel.
  3. Drag and drop your updated file into the Modified JSON panel.
  4. Keep Ignore object key order enabled (on by default) so key sequence does not trigger false differences.
  5. Click Compare JSON.
  6. Switch between Unified Diff and Side-by-Side views, filter by change type (Added, Removed, Changed, Type Changed), or copy specific property paths directly to your clipboard.

Because processing runs 100% inside your browser using client-side JavaScript, sensitive customer data, configuration files, and API secrets are never transmitted across the network.


Method 2: Compare JSON in Python with DeepDiff

Python engineers can perform deep recursive comparisons using the deepdiff package:

pip install deepdiff
import json
from deepdiff import DeepDiff

with open('file1.json', 'r') as f1, open('file2.json', 'r') as f2:
    data1 = json.load(f1)
    data2 = json.load(f2)

# Structural diff ignoring dictionary key order
diff = DeepDiff(data1, data2, ignore_order=False)

# Inspect categorized changes
print("Added keys:", diff.get('dictionary_item_added', []))
print("Removed keys:", diff.get('dictionary_item_removed', []))
print("Values changed:", diff.get('values_changed', {}))
print("Type changes:", diff.get('type_changes', {}))

Method 3: Compare JSON in Node.js / JavaScript

In JavaScript and TypeScript applications, you can compute standard RFC 6902 JSON patch deltas:

import fs from 'node:fs';
import { compare } from 'fast-json-patch';

const docA = JSON.parse(fs.readFileSync('file1.json', 'utf8'));
const docB = JSON.parse(fs.readFileSync('file2.json', 'utf8'));

// Returns an array of operations: add, remove, replace
const patch = compare(docA, docB);
console.log('JSON Delta:', patch);

Method 4: Compare JSON on the command line (jq + diff)

In bash, zsh, or Linux/macOS terminal scripts, normalize both JSON files by sorting their keys alphabetically before passing them to diff:

diff -u <(jq -S . file1.json) <(jq -S . file2.json)

The -S flag tells jq to sort all object keys deterministically, eliminating false positives caused by property order.


  • JSON Diff Tool — Compare two JSON files online with side-by-side highlighting.
  • JSON Formatter — Beautify and indent JSON before running visual diffs.
  • JSON Validator — Validate syntax and auto-repair malformed JSON payloads.
  • JSON to CSV Converter — Flatten nested JSON documents into tabular spreadsheet columns.