DevKitHub

JSON & Data

JSON Diff — Compare Two JSON Documents by Path

Paste two JSON documents to see which fields differ, by path. Key order and formatting are ignored, because they are not differences.

1 line
0 lines

This tool runs entirely in your browser. Your input is never uploaded, stored or logged.

How it works

A text diff of two JSON documents is mostly noise. Reformatting, a changed key order or a single inserted array element shifts every line that follows, and the output claims forty lines changed when one value changed. The question being asked is almost always "which fields differ", and that is a question about the parsed structure rather than about the bytes. So both documents are parsed and walked, and the report is a list of paths: what was added, what was removed, and what changed from which value to which.

Key order is irrelevant by construction, because objects are compared key by key. Formatting is irrelevant for the same reason. A type change is reported separately from a value change, and the walk stops descending once the types differ — an object becoming an array is one change, not every key removed and every index added, which is what a naive recursion produces and what makes its output unreadable.

Arrays compare by position by default, which is right when the array is ordered: a pipeline, a sequence of steps, a list of migrations. When it is really a set — tags, roles, permissions — position is meaningless and a reorder reports as every element changing. That case has a switch, and it matches elements by value instead, including whole objects. It is an option rather than a default because only you know which kind of array you have.

Common problems

Every example below is run against this tool in our test suite, so what it says here is what the tool actually does.

The left document is not valid JSON.

{"a":1,}
Why:
A trailing comma. JavaScript object literals permit one and JSON does not, so anything that builds JSON by string concatenation produces this constantly.
Fix:
Remove the trailing comma. The error names which side failed, so you are not hunting through both.

A reordered list reports as completely changed.

Why:
Arrays compare by position by default, which is correct for an ordered list and wrong for a set. Reordering a set moves every element to a new index.
Fix:
Turn on set comparison, which matches elements by value and ignores their order.

A text diff showed dozens of changes and this shows one.

Why:
The other lines differed in formatting or key order, which are not differences in the data. A text diff cannot tell those apart from real changes.
Fix:
Nothing to do — this is the difference between a structural diff and a line diff, and why the two disagree.

Frequently asked questions

How is this different from a text diff?
A text diff reports reformatting and key reordering as changes, and one inserted line shifts everything after it. This parses both documents and compares fields, so it reports only what actually differs.
Why did a reordered array come out as all changed?
Because arrays are ordered by default, which is right for a pipeline and wrong for a set of tags. Switch on set comparison and a reorder reports as no change at all.
Are the documents sent anywhere?
No. Both are parsed and compared in your browser, which matters here because the documents being compared are usually real API responses with real data in them.

Read more about this

Last updated