Guide

Compare Two JSON Files: Find Differences and JSON Patch

Short answer: to compare two JSON files properly, use a JSON diff, not a text diff: a plain text diff marks two files as different when only the key order or spacing changed. A JSON diff compares the values. Paste both documents into the JSON Diff tool and it lists each added, removed or changed value with its exact path, and it ignores key order.

Text diff versus JSON diff

{"a":1,"b":2} and {"b":2,"a":1} mean the same thing, yet a line-by-line comparison flags them. Because the tool compares parsed values, it reports only real differences. It also reports a type change, for example the number 1 becoming the string "1", which is easy to miss by eye and a common cause of API bugs.

Reading the result

Every difference comes with its path, so you can see exactly where the change is, instead of hunting through a long file. The built-in example finds three differences, which is a good way to see the output format before you use your own data.

Arrays: position or order

Arrays are where comparisons get interesting.

  • By position: item 0 is compared with item 0, and so on. Use this when order matters, such as a list of steps.
  • Ignore order: use this when an array is really a set, such as a list of tags, so that a reshuffle does not count as a change. To pull one value out of a large document before comparing, see the JSONPath cheat sheet.

JSON Patch (RFC 6902)

In position mode, the tool can express the differences as a JSON Patch, the standard from RFC 6902. A patch is a list of operations that turns the first document into the second. The tool writes add, remove and replace operations (the RFC also defines move, copy and test). This is useful when you want to send only the change to an API instead of the whole document. Patch output is not offered in ignore-order mode.

How to compare two JSON files with the JSON Diff tool

  1. Open the JSON Diff.
  2. Paste the original on one side and the changed version on the other. Check each side first with the JSON Validator if either fails to parse (see fixing JSON unexpected token errors).
  3. Choose position mode or ignore-order mode for arrays.
  4. Read the list of differences by path.
  5. In position mode, copy the JSON Patch if you need it.

Limits to know

  • It compares values, so formatting, whitespace and key order are deliberately ignored.
  • JSON Patch is available only in position mode.
  • Everything runs in your browser.

Frequently asked questions

How do I compare two JSON files online? Paste both into the JSON Diff tool.

Does key order matter? No. Objects are compared by value.

Can I ignore array order? Yes, with the ignore-order mode.

What is JSON Patch? A standard format (RFC 6902) describing the changes needed to turn one JSON document into another.

Will it notice that 1 became "1"? Yes. It reports type changes.

Related tools and guides

Sources and further reading