mygems

How to compare two JSON files and see what changed

A text diff flags reformatting and key order as changes. Here is how to compare JSON by structure with jq, jd, Python, VS Code or on a phone.

By mygems ·

A text diff of two JSON files tells you which lines changed. That's the wrong question. Reformat one file, or let a serializer write its keys in a different order, and every line differs while the data is identical. Change one value deep inside a minified file and the diff shows a single 30,000 character line. What you want is a structural diff: which keys, values and array items changed. Here's how to get one.

The fast answer

  • Quick, in a terminal: sort and format both, then diff the text: diff <(jq -S . a.json) <(jq -S . b.json).
  • A real structural diff: jd a.json b.json, which reports paths, not lines.
  • In Python: DeepDiff from the deepdiff package, with ignore_order=True when array order doesn't matter.
  • In VS Code: format both files first, then Select for Compare and Compare with Selected.
  • On a phone: a JSON app with a structural diff, not a website you paste two documents into.

Why plain diff gets it wrong

Three kinds of difference look like changes to a text diff and aren't:

  • Whitespace. One file is pretty-printed, the other minified. Every line differs.
  • Key order. {"a":1,"b":2} and {"b":2,"a":1} are the same object. JSON objects are unordered by definition, and different languages serialize them differently.
  • Number spelling. 1.0, 1 and 1e0 are the same number to most parsers, and different text.

And one kind of difference is a change but text diff handles badly: arrays. Insert an item at the top of a list of 500 records and a line diff reports 500 changes, because every item has moved down by one.

The jq shortcut

If you have jq, normalizing both files first fixes the whitespace and key order problems in one go:

diff <(jq -S . a.json) <(jq -S . b.json)

-S sorts keys, . pretty-prints. The diff you get back is now about data. It still reports array insertions badly, and it still shows lines rather than paths, but for config files and small API responses it's often enough.

On Windows without a Bash shell, write both to files and compare those:

jq -S . a.json > a.sorted.json
jq -S . b.json > b.sorted.json
fc a.sorted.json b.sorted.json

A structural diff with jd

jd is a small command-line tool made for exactly this. It compares the parsed structure and reports each change as a path:

jd a.json b.json
@ ["user","email"]
- "[email protected]"
+ "[email protected]"
@ ["items",2]
+ {"sku":"B-17","qty":1}

Useful options:

  • -set treats arrays as unordered sets, so a reordered list isn't a change.
  • -setkeys id pairs array items by their id field, so an edited record is reported as an edit rather than a removal and an addition.
  • -f patch outputs a standard JSON Patch (RFC 6902) you can apply elsewhere.

In Python

import json
from deepdiff import DeepDiff

a = json.load(open("a.json"))
b = json.load(open("b.json"))
print(DeepDiff(a, b, ignore_order=True))

The output groups changes by kind (values changed, items added, items removed, type changes) with a path to each. ignore_order=True is what you want for most lists of records. Leave it off when order is the data, such as the steps of a pipeline.

In an editor

VS Code compares any two files side by side: right-click the first in the Explorer, Select for Compare, then right-click the second, Compare with Selected. It's a text diff, so format both with Shift+Alt+F first, and it helps to sort keys too (an extension, or the jq command above).

JetBrains IDEs have the same in Compare Files, with a setting to ignore whitespace.

If you're comparing two versions of a file in git, git diff has the same weakness as plain diff. A consistent formatter on commit stops most of the noise at the source.

Watch out for these

  • IDs and timestamps that always change. An API response with a requestId or generatedAt field differs every time. Delete those fields before comparing: jq -S 'del(.requestId, .generatedAt)' a.json.
  • Large integers. JavaScript-based tools round integers above 9,007,199,254,740,991, so two different 19-digit IDs can compare as equal. jq 1.7 and Python keep them exact.
  • Invalid JSON. Most tools stop with a parse error. Fix it first; a trailing comma in one file is not a difference you want to hunt for.
  • Online diff sites. Both documents are pasted into a page you can't inspect. Fine for sample data; not for anything with tokens or customer records.

On iPhone and iPad

Comparing on a phone usually means a response you've just fetched against a file you saved last week, or a config before and after a change. Smart Json diffs the open document against another file, the clipboard or a URL. The comparison is structural, so reformatting isn't reported as a change, and array items are paired by identity when they have one, so an edited record shows up as one edit. Changed subtrees are tinted in the graph view, which makes it quick to see where in a large document the changes are.

A comparison against orders-new.json reporting 1 removed and 2 changed, paired by id, with the changed status and total fields highlighted in the graph
Three differences, found by id and shown where they sit
JSON Editor: Smart JsoniPhone & iPad

A structural diff on iPhone and iPad: against a file, the clipboard or a URL, with changes tinted in the tree and the graph. Offline.

What it does

For comparing tabular data rather than nested documents, comparing two CSV files works differently, by matching rows on a key column.

Questions

How do I compare two JSON files?
Use a structural diff, which compares the data rather than the text. In a terminal, jd a.json b.json reports changes by path. For a quick check, normalise both with jq -S . and diff the output. In Python, DeepDiff compares two loaded JSON values.
How do I compare JSON and ignore key order?
Sort the keys of both files before comparing, for example with jq -S . file.json. Structural diff tools such as jd and DeepDiff ignore key order automatically, since JSON objects are unordered.
How do I compare JSON arrays when the order doesn't matter?
Use jd with -set, or DeepDiff with ignore_order=True. To pair records by an ID field so edits show as edits, use jd -setkeys id.
Why does my JSON diff show every line as changed?
One file is formatted differently, its keys are in a different order, or it is minified onto one line. Pretty-print and sort both files the same way first, or use a structural diff that ignores formatting.
Can VS Code compare two JSON files?
Yes. Right-click one file and choose Select for Compare, then right-click the other and choose Compare with Selected. It compares text, so format both files first to avoid whitespace noise.

mygems — Builds Smart Json, a JSON editor for iPhone and iPad that shows one document as text, a tree and a graph.