How to Compare Two JSON Files and Find the Differences
A plain text comparison treats key order, indentation, and array order as differences even when the data is equivalent. Format both sides first, then compare the structure field by field to find real additions, removals, and changes.
Why plain text comparison misleads
Two JSON documents can describe the same object and still produce a wall of red lines:
- Keys appear in a different order in each file.
- One side is minified and the other is formatted, so lines do not align.
- Array elements were reordered while the set of values stayed the same.
- The number
1and the string"1"differ only by quotes but have different types. - A missing field and a field set to
nullare not the same thing.
The two documents below are nearly identical, but the second has an extra note field and a different key order:
{
"id": 42,
"status": "active",
"roles": ["admin", "editor"]
}
{
"roles": ["admin", "editor"],
"status": "active",
"id": 42,
"note": "new"
}
How to compare two JSON documents
You can use a ready-made diff tool or write a small script; both approaches start by making each side valid and consistently formatted JSON.
Compare with a JSON diff mode
hpjson.com offers format, view, and diff modes. Format both sides before reading the highlighted changes:
- Open the site and switch to diff mode.
- Paste one document on each side or select local files.
- Format both sides and confirm each one parses.
- Review added, removed, and changed fields; compare arrays by index or business ID.
Everything runs locally in the browser, data is not uploaded to a server, and parsing happens in a Web Worker so the interface stays responsive. The per-file limit is 100MB, and comparing two large files holds both sides in memory, so check the available memory on the device.
Write a small structural diff in JavaScript
If the comparison belongs in a script, a recursive function can walk plain objects and compare arrays and scalar values by content:
function diff(a, b, path = "") {
const changes = [];
const keys = new Set([...Object.keys(a), ...Object.keys(b)]);
for (const key of keys) {
const before = a[key];
const after = b[key];
const fullPath = path ? `${path}.${key}` : key;
if (!(key in a)) {
changes.push({ path: fullPath, type: "added", value: after });
} else if (!(key in b)) {
changes.push({ path: fullPath, type: "removed", value: before });
} else if (
before && after &&
typeof before === "object" && typeof after === "object" &&
!Array.isArray(before) && !Array.isArray(after)
) {
changes.push(...diff(before, after, fullPath));
} else if (JSON.stringify(before) !== JSON.stringify(after)) {
changes.push({ path: fullPath, type: "changed", before, after });
}
}
return changes;
}
console.log(diff(
{ id: 1, tags: ["a"] },
{ id: 1, tags: ["a", "b"], active: true }
));
The output reports that tags changed and active was added. Note that JSON.stringify makes array comparison order-sensitive; if an array represents a set, sort it or match items by ID before comparing.
Reduce noise before comparing
- Agree on a key order or format both sides first.
- Match array items by a stable business key instead of their index.
- Ignore timestamps, request IDs, and other fields that change on every run.
- Validate both documents first so a syntax error on one side does not invalidate the result.
- Save snapshots with a consistent schema to avoid false differences from temporary fields.
FAQ
Does key order count as a difference? Not in a structural comparison, but a text diff will report it. Choosing the right comparison method matters.
What if array order changed but the contents are the same? Sort by a stable ID before comparing; serializing directly will mark the arrays as different.
Is my data uploaded? No. hpjson.com works locally in the browser and parses data in a Web Worker.
Should 1 and "1" be treated as equal? No. A number and a string are different JSON types, and a strict comparison reports them as different.
Can two 100MB files be compared? The per-file limit is 100MB, and parsing both sides uses more memory. Split the files or compare only the changed sections.
To compare in practice, open the JSON formatter and choose diff mode, and see the FAQ for more usage notes. The tool is free, currently has no ads, and is available in Chinese and English.