How to Compare Two JSON Files?

You have two JSON files and you want to know what changed. Maybe one is an old API response and one is new. Maybe it is a config file before and after an update.

The first thing most people try is a text diff. That is the line-by-line compare you see in Git and in most editors. With JSON, it often shows lots of red and green lines, even when almost nothing changed. This guide explains why. It also shows you how to get a short list of the real changes.

Quick answer: To compare two JSON files, compare their data, not their text. A JSON diff tool reads both files. It lists every value that was added, removed or changed, together with its path. Key order and spacing don’t show up as false changes.

Why a normal text diff is noisy for JSON

A text diff compares lines of characters. It has no idea what the JSON means. So two files can hold exactly the same data and still look completely different to it. Look at these two files:

// old.json
{ "name": "Ada", "age": 36 }

// new.json
{
  "age": 36,
  "name": "Ada"
}

The data is the same. A text diff still marks every line as changed. There are three common reasons:

  • Key order. In a JSON object, the order of the keys has no meaning. Many programs write keys in a different order each time they save.
  • Formatting. Spaces, tabs and line breaks don’t change the data. One file may be minified (all on one line) while the other is indented.
  • Number style. Most programs read 1 and 1.0 as the same number, but the text is different.

How to compare JSON by path

A better way is to compare the JSON as data. To do that, you need paths. A path is the address of one value inside the document:

  • $ means the whole document.
  • $.user.email means the email key inside the user object.
  • $.roles[2] means the third item in the roles list. Counting starts at 0.

A structural diff compares the data, not the text. It goes through both documents and compares the value at each path. Every difference is put into one of three groups:

  • Added — the path exists only in the new file.
  • Removed — the path exists only in the old file.
  • Changed — the path exists in both files, but the value is different.

Here is an example:

// old.json
{
  "user": { "name": "Ada", "email": "ada@old.example" },
  "roles": ["admin", "editor"],
  "active": true
}

// new.json
{
  "active": true,
  "user": { "email": "ada@new.example", "name": "Ada" },
  "roles": ["admin", "editor", "viewer"],
  "plan": "pro"
}

A structural diff reports just three things:

Changed  $.user.email  "ada@old.example" → "ada@new.example"
Added    $.roles[2]    "viewer"
Added    $.plan        "pro"

The keys moved around, but that is not listed, because it doesn’t change the data. The JSON Diff tool works this way. Paste the original JSON on the left and the new JSON on the right. The differences appear below, grouped as added, removed or changed. Each one shows its path and the old and new values. It runs in your browser, so your files are not sent anywhere.

Comparing arrays: in order or ignoring order?

Arrays (lists) need extra thought. By default, a JSON diff compares them position by position. It compares item 0 with item 0, item 1 with item 1, and so on. So ["a", "b"] and ["b", "a"] show two changes.

That is usually right. In many lists, the order matters. Think of steps in a process, or search results ranked from best to worst.

But sometimes the order means nothing. A list of tags or permission names is a good example. Then you want to ignore the order. In JSON Diff, turn on Ignore array order. Items are then matched by their content, wherever they sit in the list.

Watch out for one thing. If one item is added at the start of a list, every item after it moves by one place. A position-by-position compare then reports a change at almost every position. If you see that, check whether ignoring order makes sense for that list.

Tip: sort the keys first if you need a text diff

Sometimes you have to use a text diff, for example in a code review or a Git history. You can still make it much cleaner. Give both files the same key order and the same formatting first.

  1. Run both files through Sort JSON Keys. It orders every object’s keys from A to Z, at every level. Arrays keep their order, and no values change.
  2. Format both files with the same indentation, for example 2 spaces.
  3. Now compare them. The text diff shows only real changes.

If you save JSON files in Git, sorting keys before each commit keeps future diffs small and easy to read.

How to compare JSON on the command line with jq

jq is a free command-line tool for reading and changing JSON. Its -S option sorts object keys, and the . means “the whole input”. jq also prints the result with the same indentation every time. So it works well together with the normal diff command:

diff <(jq -S . old.json) <(jq -S . new.json)

The <( … ) part is a shell feature in bash and zsh. It passes jq’s output to diff as if it were a file. If your shell doesn’t support it, save sorted copies first:

jq -S . old.json > old.sorted.json
jq -S . new.json > new.sorted.json
diff old.sorted.json new.sorted.json

Keep two limits in mind. jq -S sorts keys, but it does not sort arrays. And the result is still a line diff, so you see changed lines, not paths.

After the diff: combining two versions

Sometimes you don’t just want to see the changes. You want to bring two files together. A common case is a default config plus a smaller file of overrides. JSON Merge does a deep merge. This means objects are merged key by key, at every level. You choose how arrays are combined. You also choose which side wins when both files have a different value at the same place.

Common questions

Does key order matter when comparing JSON?

No. Key order in a JSON object has no meaning. A JSON diff tool ignores it. Only a plain text diff treats it as a change.

Why does my diff show changes when the data looks the same?

Usually it is formatting, key order or a list in a different order. Also check for hidden differences, like a trailing space inside a string, or 1 versus "1". One is a number and the other is a string, so they are not equal. Our guide to JSON data types explains the difference.

Can I compare JSON files online without uploading them?

Yes. JSONPilot’s JSON Diff runs in your browser. The files you paste stay on your computer and are not sent to a server.

How do I compare JSON when the arrays are in a different order?

Use a diff that can ignore array order, such as JSON Diff with “Ignore array order” turned on. Sorting keys alone won’t help, because sorting only changes object keys, not array items.