Skip to content

How to compare two JSON files

Compare JSON by its data rather than its text, so key order and formatting don't show up as changes, online or in code.

Guide5 min readUpdated

Comparing two JSON files sounds like a job for a normal diff, but JSON isn't plain text. Two files can hold exactly the same data and still look completely different, because keys are in another order or the indentation changed. Take these two:

{"name":"Ann","age":30,"tags":["a","b"]}
a.json
{
  "age": 31,
  "name": "Ann",
  "tags": ["a", "b"],
  "active": true
}
b.json

A line-by-line diff marks every line as changed. In reality there are only two changes: age went from 30 to 31, and active was added.

Compare JSON online

Paste the two documents into JSON Diff. It compares the data, not the text, so key order and whitespace never show up as changes. You get each addition, removal and change with its path, a unified or side-by-side view, and the differences as a JSON Patch you can copy. Both documents stay in your browser, which matters when you're comparing API responses or production config.

On the command line with jq

Normalise both files with jq -S (sort keys, consistent formatting) and diff the results:

diff <(jq -S . a.json) <(jq -S . b.json)
2c2,3
<   "age": 30,
---
>   "active": true,
>   "age": 31,
Only the real changes remain.

In Python

Parsed JSON objects become dictionaries, and dictionary equality ignores key order:

import json

with open("a.json") as f: a = json.load(f)
with open("b.json") as f: b = json.load(f)

print(a == b)                                  # False: the data differs
print({"x": 1, "y": 2} == {"y": 2, "x": 1})    # True: key order doesn't matter
print([1, 2] == [2, 1])                        # False: array order does

That tells you whether they differ, not where. For a list of differences, libraries such as DeepDiff report each changed path.

In JavaScript

Don't compare JSON.stringify output: it depends on key order, so equal objects can produce different strings. In Node.js, use the built-in deep comparison:

import { isDeepStrictEqual } from "node:util";

isDeepStrictEqual({ x: 1, y: 2 }, { y: 2, x: 1 });                  // true
JSON.stringify({ x: 1, y: 2 }) === JSON.stringify({ y: 2, x: 1 });  // false

Describing the differences as JSON Patch

JSON Patch (RFC 6902) is the standard way to write differences down: a list of operations that turn the first document into the second. For the example above:

[
  { "op": "replace", "path": "/age", "value": 31 },
  { "op": "add", "path": "/active", "value": true }
]

Patches are useful for audit logs, syncing data and HTTP PATCH requests. JSON Diff generates one for any two documents.

Tips for cleaner comparisons

  • Arrays are ordered. If order doesn't matter in your data (like a list of tags), sort both arrays first, or use JSON Diff's option to ignore array order.
  • Ignore noise. Timestamps and generated IDs change on every response; remove them before comparing so the real differences stand out.
  • Format first when reading by eye. The JSON Formatter makes minified responses readable, and Deep Search finds a key's path in a big document.

Frequently asked questions

Does key order matter in JSON?

No. JSON objects are unordered, so {"a":1,"b":2} and {"b":2,"a":1} contain the same data. Array order does matter: [1,2] and [2,1] are different.

What is JSON Patch?

A standard format (RFC 6902) for describing changes to a JSON document as a list of operations like add, remove and replace, each with a path. Applying the patch to the first document produces the second.

Can I compare very large JSON files?

Yes. A structural diff only needs both documents to be valid JSON. In the browser it handles files of several megabytes; for much larger files, jq on the command line is a good option.

Tools used in this guide

More guides