What a structural comparison is
A text diff compares lines of characters. That suits source code, but it is noisy for JSON. Reformat a file, reorder two keys or change the indentation, and a text diff flags lines that mean exactly the same thing.
A structural (semantic) comparison parses both documents first and then compares the values they contain. Whitespace, line breaks and key order drop out, so you see only real changes: a value that was added, removed or changed.
How to use JSON Compare
- Paste your first document into Original JSON.
- Paste the second document into Changed JSON.
- Read the summary line and the table below the editors. The result updates as you type.
- Select Copy as text if you want a plain report to paste into a ticket or message.
The tool opens with an example pair of package.json-like documents. Pasting replaces them. Swap sides exchanges the two editors, so additions become removals and the other way round. Clear empties the editors.
Everything runs in your browser. Nothing is uploaded.
If either document is not valid JSON, the tool shows the syntax error with its line and column, and tells you which side it is in.
Reading the results
The summary line gives the totals, for example 3 differences: 1 added, 1 removed, 1 changed. The table below has four columns: Change (Added, Removed or Changed), Path, Original and Changed. The tool lists up to 2000 differences.
Copy as text produces a plain report with one line per difference:
+ $.license: "MIT"
- $.private: true
~ $.version: "1.0.0" → "1.1.0"
Paths use JSONPath. Some examples:
$.scripts.testis thetestkey insidescripts.$.keywords[2]is the third item of thekeywordsarray.$["key with spaces"]is a key that cannot be written with dot notation.
To use a path in jq, replace the leading $ with a dot. $.scripts.test becomes .scripts.test.
Objects are compared by key, so key order never counts as a difference. A value whose type changes, such as the string "1" against the number 1, or {} against [], is reported as changed.
How arrays are compared
Arrays are compared by position. Index 0 is matched with index 0, index 1 with index 1, and so on. Extra items at the end show as added or removed.
This means an insert at the start of an array shifts everything after it. Take these two arrays:
["b", "c"]
["a", "b", "c"]
The comparison reports index 0 as changed from "b" to "a", index 1 as changed from "c" to "b", and a new item at index 2. You inserted one item, but the tool shows three differences.
The tool does not detect moved items, and it does not treat arrays as sets.
Numbers and strings
Numbers are compared by exact decimal value:
1,1.0and1e0are equal.0.5and5e-1are equal.1.00000000000000001and1are different. JavaScript would round both to the same double, but this tool does not.- Big integers keep every digit.
Strings are compared by their decoded value. The escaped form "A" equals "A".
Comparing JSON in code
To get a similar result in a shell, sort the keys with jq and then run an ordinary text diff:
diff <(jq -S . a.json) <(jq -S . b.json)
The -S option sorts object keys, so key order does not matter. The comparison afterwards is still line by line, and it reports lines rather than paths.
In JavaScript, a deep-equal function tells you whether two values match. Node’s util.isDeepStrictEqual is one example:
import { isDeepStrictEqual } from 'node:util';
const a = JSON.parse(originalText);
const b = JSON.parse(changedText);
console.log(isDeepStrictEqual(a, b)); // true or false
It returns only true or false. It does not tell you what differs or where. Note too that JSON.parse turns numbers into doubles, so very precise decimals and big integers can lose digits before the comparison begins.