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):
- In the Explorer, right-click the first file and choose Select for Compare, then right-click the second and choose Compare with Selected.
- Command Palette → File: Compare Active File With… and pick the other file.
- To compare with JSON you copied, such as an API response: File: Compare Active File with Clipboard (Ctrl+K C, ⌘K C on macOS).
- 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:
status:"pending"→"shipped"updatedAt: a new timestamp (usually noise)- Item
id: 12removed - Item
id: 11:qty2 → 3 total: the number67became the string"55.50"
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.
| Step | jq filter | Lines +/− | What is left |
|---|---|---|---|
| Raw files | — | +10 / −15 | Key order, tag order, item order, timestamp and the real changes |
| Sort object keys | jq -S . | +10 / −15 | Key order is gone, but in these files the reordered lists still dominate |
| Also sort arrays | jq -S '(.items |= sort_by(.id)) | (.tags |= sort)' | +4 / −9 | Timestamp and the four business changes |
| Also drop the timestamp | jq -S 'del(.updatedAt) | (.items |= sort_by(.id)) | (.tags |= sort)' | +3 / −8 | Only 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
- No paths. A changed line
"qty": 3does not say which item it belongs to; you read the surrounding lines. - Type changes look like value changes.
"total": 67→"total": "55.50"is one line pair; nothing flags that a number became a string. - Removed items spread over lines. The deleted item is five lines in the diff above.
- The filter is per document shape. A new array or a new noisy field means editing the jq filter again.
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.
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).