JSON Diff: Comparing JSON by Structure, Key Order and Array Index

Last updated: · 8 min read

An ordinary line-by-line text diff is often misleading for JSON: if the keys were reordered or the indentation changed, nearly every line shows up as different even though the content is the same. A structural comparison parses both documents first and compares the values, so only real differences remain. The JSON Diff tool compares two documents this way and lists every difference on its own line, with a JSON Pointer (RFC 6901) path showing where in the document it sits. This guide explains what counts as a difference and what does not, why arrays are matched by index, how to read the paths and which mistakes are common when interpreting the result. The output shown is the tool's real result, and the comparison runs in your browser.

Formula

Rules:
1. Both documents are parsed; whitespace, indentation and the order of keys in objects are not differences.
2. Objects are matched by key name: a key only on the left is listed as removed, a key only on the right as added; for keys on both sides the values are compared.
3. Arrays are matched by index: element 0 with element 0, element 1 with element 1. The extra elements of the longer array count as added or removed.
4. Numbers are compared by value, not by spelling: 1, 1.0 and 10e-1 are the same. The comparison is exact; 9007199254740993 and 9007199254740992 are different.
5. If the two values have different JSON types (string, number, boolean, null, object, array), a single "type changed" line is written and the tool does not descend further.
6. The location of each difference is given as a JSON Pointer: /address/city is an object member, /tags/2 the third element of an array. In a key, ~ is written as ~0 and / as ~1.
7. The first line sums up the differences by kind; when there are none, a one-line message is shown.

Left JSON:
{"name":"Ada","age":36,"role":"admin","tags":["a","b"],"address":{"city":"London"}}

Right JSON:
{"age":37,"name":"Ada","tags":["a","b","c"],"address":{"city":"Paris","zip":"75001"},"active":true}

Output:
Differences: 6 (added 3, removed 1, changed 2, type changed 0)
Changed /age: 36 → 37
Removed /role: "admin"
Added /tags/2: "c"
Changed /address/city: "London" → "Paris"
Added /address/zip: "75001"
Added /active: true

This is a comparison algorithm, not a formula. The tool walks both documents together, starting at the root. Ignoring key order follows from the definition of JSON: RFC 8259 describes an object as an unordered collection of name/value pairs and an array as an ordered sequence of values. Order therefore carries no meaning in an object and does in an array. Values are shown in the lines as minified JSON; strings keep their quotes, so the string "1" and the number 1 can be told apart in the output.

What the inputs mean

Left JSON
The starting side of the comparison, usually the old version or the expected value. It must be a valid JSON document; an object, an array or a single value all work.
Right JSON
The other side, usually the new version or the value actually received. "Added" lines show what exists here but not on the left; "removed" lines show what exists on the left but not here.

Worked example: Comparing two versions of a user record

  1. In the right document the age key comes before name; key order is not a difference, so name is not listed at all. The first line gives the summary: Differences: 6 (added 3, removed 1, changed 2, type changed 0)
  2. age exists on both sides with a different value: Changed /age: 36 → 37
  3. role exists only on the left: Removed /role: "admin"
  4. The first two elements of tags are identical; the third element on the right is reported as added at index 2: Added /tags/2: "c"
  5. address is a nested object, and the path shows both levels: Changed /address/city: "London" → "Paris" and Added /address/zip: "75001"
  6. active exists only on the right: Added /active: true
  7. Order and spelling only: for left {"a":1,"b":[1,2,3]} and right {"b":[1,2,3],"a":1.0} the result is: No differences: the two JSON documents are structurally identical. 1 and 1.0 are the same number.
  8. Removing an element from the front of an array: for left {"list":["x","y","z"]} and right {"list":["y","z"]} the summary is Differences: 3 (added 0, removed 1, changed 2, type changed 0) Because elements are matched by index, the tool reports two changes and one removal rather than a single removal: Changed /list/0: "x" → "y", Changed /list/1: "y" → "z" and Removed /list/2: "z"
  9. Escapes in paths and a type change: for left {"a/b":1,"n":"1"} and right {"a/b":2,"n":1}, the key a/b is written with ~1 in the path (Changed /a~1b: 1 → 2), and n, which turned from a string into a number, is reported as a different type: Type changed /n: string → number ("1" → 1)

Result: Result of the first comparison: Differences: 6 (added 3, removed 1, changed 2, type changed 0) The name field, whose position changed, is not in the list; each line shows one path with the old and the new value at that path.

🆚 JSON Diff – Compare Two JSON Documents

Free, no sign-up; your data stays in your browser.

Open the calculator →

How to read the result

A structural comparison is useful for finding how a configuration file differs between two environments, seeing how an API response changed between versions and comparing expected with actual output in tests. Matching arrays by index is simple and predictable, but it has a limit: the tool does not detect that elements were moved or inserted. If the order of an array of records does not matter, sort both sides by the same field (an id, for example) before comparing. The output is a list written to be read, not a JSON Patch (RFC 6902) document; JSON Patch uses the same path syntax but is a JSON array of operations such as add, remove and replace that a machine can apply. A key that is absent and a key whose value is null are also different things and are listed as a difference.

Common mistakes

  • Comparing an array whose order is irrelevant without sorting it. When the same elements arrive in another order, every position looks changed; sort both sides by the same criterion first.
  • Expecting one line for a single element inserted at the start or in the middle of an array. Every later element shifts by one position, so all of them are reported as changed.
  • Treating "1" and 1 as equal. One is a string and the other a number; the tool reports this as a type change, and it usually points to a real incompatibility.
  • Treating null and a missing key as the same. {"a":null} and {} are different documents; if a key is null on one side and absent on the other, you get an added or removed line.
  • Reading ~1 and ~0 in a path as part of the key. In JSON Pointer, ~1 stands for a slash and ~0 for a tilde; the path /a~1b refers to a single key named a/b.
  • Expecting strings that merely look alike to be equal. Strings are compared character by character; trailing whitespace, letter case or a different Unicode spelling of the same letter all count as differences.
  • Comparing documents that contain duplicate keys. If a key occurs twice in the same object, only the last occurrence is used; the earlier ones are silently ignored.

Frequently asked questions

Does array order matter in a JSON diff?

Yes. In JSON an array is an ordered sequence of values; [1,2] and [2,1] are different documents. The tool matches arrays by index and does not separately detect that elements changed places. For lists whose order carries no meaning, sort both sides first.

Why is key order not a difference?

RFC 8259 defines an object as an unordered collection of name/value pairs and notes that parsers differ in whether they expose member order at all. {"a":1,"b":2} and {"b":2,"a":1} therefore carry the same data, and the tool reports no difference between them.

How is this different from a text diff?

A text diff compares lines; when indentation, line endings or key order change, it shows differences even though the content is the same. A structural diff compares the parsed values and reports each difference with its path in the document. Use a text diff when formatting matters to you, and a structural diff when only the data does.

How do I read a JSON Pointer path?

The path starts at the root and each / goes one level down: /address/city is the city member of the address object, and /tags/2 is the element at index 2 of the tags array, counting from zero. A / inside a key is written as ~1 and a ~ as ~0. If the root itself changed, (document root) is shown in place of a path.

Why are 1 and 1.0 treated as the same?

They are two spellings of the same number; JSON conveys the number's value, not how it was written. The tool compares numbers by their exact value without converting them to floating point, so 1 and 1.0 are equal while very large integers that differ only in the last digit are still told apart.

Does the tool produce a JSON Patch (RFC 6902)?

No. The output is a plain list for people to read. A JSON Patch is a JSON array, of media type application/json-patch+json, made of add, remove, replace, move, copy and test operations. Because the paths use the same syntax (JSON Pointer), writing a patch by hand from the listed lines is straightforward.

Are my documents sent to a server?

No. The comparison runs in your browser. It is still good practice to remove access tokens and personal data from documents before pasting them anywhere.

The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.

Sources

Choose your cookie preference to continue

Records the site needs to work (theme, language, favourites) stay in your browser.

This site is free and funded by ads.

With your consent, Google Analytics cookies measure visits and Google AdSense cookies are used to show ads; the data is processed on Google servers abroad.

If you reject, the site works just the same.

Cookie Policy · KVKK notice