velkaro.Runs in your browser

JSON field notes · 06

Compare two JSON files in VS Code, jq or git diff

VS Code and git compare JSON as lines of text. On our test pair that turned 5 real changes into 25 changed lines; three jq steps bring it down to the lines that matter.

Compare two JSON files in VS Code

VS Code opens two files in its diff editor in four documented ways (Compare files, command line):

  1. In the Explorer, right-click the first file and choose Select for Compare, then right-click the second and choose Compare with Selected.
  2. Command Palette → File: Compare Active File With… and pick the other file.
  3. To compare with JSON you copied, such as an API response: File: Compare Active File with Clipboard (Ctrl+K C, ⌘K C on macOS).
  4. From a terminal: code --diff order-a.json order-b.json (short form -d).

The diff editor compares the files as text, line by line. That is quick for a config file you edited by hand, but with JSON from two different sources it shows reordered keys and reordered lists as changes. The fix is to normalize both files first, and jq does that in one command. The numbers below use git’s line diff on the same files; we did not run VS Code itself, so its exact line grouping may differ.

The test files and the raw line diff

Download order-a.json and order-b.json. Five things really changed between them:

Three more differences change nothing in the data: the top-level keys id and status swapped places, the two tags swapped places, and item id: 13 moved from the end of items to the start.

git diff --no-index --stat order-a.json order-b.json
 1 file changed, 10 insertions(+), 15 deletions(-)

25 changed lines for 5 real changes. git diff --no-index compares any two files, even outside a repository (git-diff documentation).

Normalize with jq, step by step

jq 1.7.1 and git 2.50.1. Each step writes both files to a normalized copy and diffs those. Options are from the jq 1.7 manual.

Stepjq filterLines +/−What is left
Raw files—+10 / −15Key order, tag order, item order, timestamp and the real changes
Sort object keysjq -S .+10 / −15Key order is gone, but in these files the reordered lists still dominate
Also sort arraysjq -S '(.items |= sort_by(.id)) | (.tags |= sort)'+4 / −9Timestamp and the four business changes
Also drop the timestampjq -S 'del(.updatedAt) | (.items |= sort_by(.id)) | (.tags |= sort)'+3 / −8Only status, qty, the removed item and total
F='del(.updatedAt) | (.items |= sort_by(.id)) | (.tags |= sort)'
jq -S "$F" order-a.json > a.norm.json
jq -S "$F" order-b.json > b.norm.json
git diff --no-index a.norm.json b.norm.json    # or: code --diff a.norm.json b.norm.json

Sorting keys alone (--sort-keys / -S) changed nothing here: the two keys that swapped places were already a small part of the diff, and the list reordering produced the rest. Sorting each list by a stable field is what removes the noise. sort_by(.id) needs every item to have an id; pick the field that identifies items in your data.

Yes/no check for scripts and CI

jq -n -e --slurpfile a order-a.json --slurpfile b order-b.json '$a == $b'
false          # exit status 1

# same document, different formatting
true           # exit status 0

-e (--exit-status) sets the exit status from the last output, so the command can fail a CI job when two responses differ. Apply the same normalization filter to both sides before comparing if order or timestamps should not count.

Where a line diff stops helping

A structural JSON diff reports each change once, with its path. On the same files Velkaro gives 12 changes by position and 4 with “Match array items by: id”, “Ignore array order” and “Ignore keys: updatedAt”, including $["items"][?@["id"]==11]["qty"] 2 → 3 and a labelled type change on total. Its “Line by line” view shows the sorted-key text diff too, and Download .diff saves it for git apply or a code review.

Compare JSON by path instead

Questions

How do I compare two JSON files in VS Code?

Right-click the first file in the Explorer and choose Select for Compare, then right-click the second and choose Compare with Selected. From a terminal, run code --diff first.json second.json.

Can VS Code ignore key order when comparing JSON?

The diff editor compares text lines, so keys in a different order show as changes. Sort the keys first, for example with jq -S, and compare the sorted copies.

How do I diff two JSON files with jq?

jq has no diff command. Normalize both files with the same filter, such as jq -S, and compare the results with git diff --no-index or diff. For a yes/no answer use jq -n -e --slurpfile a a.json --slurpfile b b.json '$a == $b'.

Why does jq -S not remove all differences?

It sorts object keys but keeps array order. If lists come back in a different order, sort them by a stable field too, for example .items |= sort_by(.id).

Related