By · Updated

JSON Diff

Compare JSON Documents by Path, Ignoring Key Order, Export an RFC 6902 Patch

No ratings yet

OneTask is free, with no sign-up, and built by one person.
If this tool saved you time, a coffee helps keep new ones coming.

How the JSON Diff works

This JSON diff compares two documents by their data, not their text. Paste the original in the main box and the changed version below it to see every added, removed and changed value with its path. Key order and whitespace are ignored, and the result can be exported as a JSON Patch.

A plain text diff is the wrong tool for JSON. Reformat a file, sort its keys or minify it and a line-based diff reports nearly every line as changed, even though the data is identical. Meanwhile a single changed number on a long minified line shows up as one huge replaced line with the real change buried inside it. Comparing the parsed structure fixes both problems.

How the JSON diff works

Both inputs are parsed, then walked together from the root:

The Added, Removed and Changed readings count each kind of difference. If there are none, the tool says the documents are identical in content.

Reading the change list

Each line starts with a symbol and a path:

Paths use JSONPath style: $ is the root, .key follows a key and [n] an array position counted from zero. Additions use positions in the changed document, and removals use positions in the original, so you can find each one in the file it belongs to. Long values are shortened to keep the list readable.

Exporting a JSON Patch

JSON Patch, defined in RFC 6902, is a standard format for describing changes as a list of operations. Each operation has an op (add, remove or replace), a path written as a JSON Pointer from RFC 6901, and a value where needed. Pointers use slashes, so $.items[1].qty becomes /items/1/qty, with ~ and / inside keys escaped as ~0 and ~1.

Operations in a patch apply in order, and each one sees the result of the ones before it. Removing item 2 from an array shifts every later item down by one. The tool accounts for this, so array positions in the patch are correct for the state at that step rather than for the original file. Applying the patch to the original with any RFC 6902 library produces the changed document.

Patches are useful well beyond comparison. PATCH endpoints in many REST APIs accept them, they make compact audit logs, and Kubernetes accepts them through kubectl patch --type=json.

Where comparing JSON helps

Some everyday cases where a structural diff saves time:

Things to watch for

Numbers are compared as JavaScript numbers, so 1.0 and 1 count as equal and integers beyond about nine quadrillion may lose precision. Very large arrays, where the two lengths multiplied pass four million, fall back to comparing items position by position to keep the page responsive; an insertion near the start of such an array then shows as many changes. If a key appears twice in one object, the parser keeps the last value, which is also what your application will see.

For arrays where order carries no meaning, such as a set of tags, consider sorting both documents first. Otherwise a simple reorder shows as a set of changes, because to JSON an array's order is part of its value.

Privacy

Both documents are parsed and compared in your browser. Nothing is uploaded, so the tool is safe to use with production data, customer records or configuration that contains secrets.

FAQ

Does key order matter?

No. Objects are compared by key, so {"a":1,"b":2} and {"b":2,"a":1} are identical. Whitespace, indentation and line breaks are also ignored. Array order does matter, because JSON treats arrays as ordered lists.

Why is a moved array item shown as removed and added?

The array comparison looks for the longest sequence of items both arrays share in order. An item that moved breaks that sequence, so it appears as a removal at the old position and an addition at the new one.

Can I apply the JSON Patch output directly?

Yes. The patch follows RFC 6902 and its array positions account for earlier operations, so any standard JSON Patch library, or an API that accepts patches, will turn the original into the changed document.

What happens with invalid JSON?

The tool tells you which of the two documents failed to parse and why, using the browser's own error message. Fix that document first, since a structural comparison needs both sides to be valid JSON.

See also: all Data & Encoding tools, Dev Utilities, Formatters & Validators, Security & Identifiers, SEO, Accessibility, AI.

Related tools