How to Diff Two JSON Files

JSON diffs come up constantly — comparing API responses, config versions, or test fixtures. Here are three approaches depending on your situation.

Option 1: Use a visual diff tool (quickest)

For a one-off comparison, paste both JSON blobs into the diff checker. It highlights added, removed, and changed lines side-by-side. Pretty-print both files first so structural changes don't get buried in formatting noise.

To pretty-print before diffing, run:

jq . file1.json > file1-pretty.json
jq . file2.json > file2-pretty.json

Then diff the pretty-printed versions.

Option 2: Command-line diff

For files already on disk, diff works fine:

diff <(jq --sort-keys . file1.json) <(jq --sort-keys . file2.json)

The --sort-keys flag normalizes key order so you only see semantic differences, not reordering. Without it, {"a":1,"b":2} and {"b":2,"a":1} look different even though they're equivalent.

For a friendlier output:

diff -u <(jq --sort-keys . file1.json) <(jq --sort-keys . file2.json)

Option 3: JavaScript for deep comparison

When you need a diff in code — unit tests, CI checks, migration scripts:

function diffJSON(a, b, path = "") {
  const keys = new Set([...Object.keys(a), ...Object.keys(b)]);
  const changes = [];
  for (const key of keys) {
    const fullPath = path ? `${path}.${key}` : key;
    if (!(key in a)) {
      changes.push({ type: "added", path: fullPath, value: b[key] });
    } else if (!(key in b)) {
      changes.push({ type: "removed", path: fullPath, value: a[key] });
    } else if (typeof a[key] === "object" && a[key] !== null) {
      changes.push(...diffJSON(a[key], b[key], fullPath));
    } else if (a[key] !== b[key]) {
      changes.push({ type: "changed", path: fullPath, from: a[key], to: b[key] });
    }
  }
  return changes;
}

const changes = diffJSON(objA, objB);
console.log(changes);
// [{ type: "changed", path: "user.name", from: "Alice", to: "Bob" }]

This gives you a structured list of changes you can assert on or log.

Comparing arrays

Arrays are trickier because element order matters. The snippet above treats arrays as values, so [1,2] vs [2,1] shows as a single changed entry. For element-level array diffing you'll want a library like deep-diff or jsondiffpatch.

What to watch for

  • Key order — sort keys before comparing to avoid false positives.
  • Type coercion"1" (string) vs 1 (number) are different in JSON.
  • Null vs missing{"key": null} and {} are semantically different.

Paste your two JSON objects into the diff checker to compare them instantly. For a broader look at diffing text files in general, see how to compare two files.

Got a config file to check?

Open the config toolkit →