Compare Two JSON Documents: Methods, Pitfalls and How to Read the Results

How to compare two JSON documents: text diffs vs structural comparison, key order, arrays, numbers and Unicode, plus copy-paste, accessibility and privacy checks for online tools.

Comparing two JSON documents sounds simple, but the answer depends on what counts as a difference. Two files can look completely different line by line and still hold the same data. Two values can also look identical on screen and still be unequal. This guide explains what a JSON comparison checks, how the main approaches differ, and how to tell whether a comparison result is reliable for your use.

What a JSON comparison actually checks

A comparison does not change either document. It produces a report: equal or not equal, a list of changed lines, or a list of changed paths and values. What ends up in that report depends on whether the tool compares the raw text or the parsed data.

A text comparison treats JSON as ordinary lines of characters. Every change in spacing, indentation, line endings or key order shows up as a difference. A structural (semantic) comparison parses both documents first and compares the resulting objects, arrays, strings, numbers, booleans and nulls. Formatting disappears during parsing, so only data differences remain.

Differences that are formatting only

  • Whitespace, indentation and line breaks between tokens.
  • The order of keys inside an object. The JSON specification (RFC 8259) defines an object as an unordered collection of name/value pairs.
  • Equivalent escape forms in strings, such as \u00e9 versus a literal é, or / versus /. Both decode to the same characters.

Differences that change the data

  • Array order. ["a", "b"] and ["b", "a"] are different arrays.
  • Value type. The string "1" is not the number 1, and true is not the number 1.
  • A key set to null versus a key that is missing entirely.
  • Different string content, including case and invisible characters.

Gray areas that depend on the parser

Numbers: the JSON grammar defines how numbers are written, not when two numbers are equal. 1, 1.0 and 1e0 are different text, and many parsers treat them as the same value. RFC 8259 also warns that many implementations use IEEE 754 double-precision numbers. Integers outside the range -(2^53)+1 to 2^53-1 may be rounded. Two different large IDs could then compare as equal after parsing.

Duplicate keys: RFC 8259 says object names SHOULD be unique, and that software behaves unpredictably when they are not. Python's json module, for example, documents that it keeps only the last value for a repeated name. A structural comparison may therefore silently ignore a duplicate that a text comparison would show.

Unicode: JSON parsers compare strings by code points and do not apply Unicode normalization. An é stored as the single code point U+00E9 and an é stored as e plus a combining accent (U+0301) look the same but compare as unequal. Zero-width characters and bidirectional control characters also look like nothing on screen but still count as content. A diff can flag a line as changed with no visible reason. Escaping non-ASCII characters before comparing makes these differences visible. jq's -a option and Python's default json.dumps output both do this.

Which comparison approach fits your goal

Approach | What it compares | Ignores formatting and key order | Output | Best suited to
Plain text diff | Raw lines | No | Changed lines | Reviewing a file exactly as committed or sent
Normalized text diff | Lines after sorting keys and re-indenting | Yes | Changed lines, readable | Quick human review with standard diff tools
Structural diff | Parsed values by path | Yes | Changed paths and values | Finding real data changes in large or reordered documents
Patch output (JSON Patch, RFC 6902) | Parsed values | Yes | Machine-applicable list of operations | Recording or applying changes programmatically
Equality check in code | Parsed values | Yes | True or false | Automated tests and validation

Use a plain text diff when the exact bytes matter. Examples include a signed payload, a fixture that must match byte for byte, or a review of formatting changes. Use a normalized diff or a structural diff when you only care whether the data changed. A structural diff is easier to read when documents are large or deeply nested, because it reports a path such as /user/roles/2 instead of a line number.

Patch formats are useful when another program needs to apply the changes. JSON Patch (RFC 6902) lists operations such as add, remove and replace, and uses JSON Pointer paths (RFC 6901). JSON Merge Patch (RFC 7396) is simpler, with two documented limits. It replaces arrays as a whole instead of editing individual elements, and it uses null to mean "remove this key," so it cannot set a value to null. Different diff generators can produce different but equivalent patches for the same change.

Arrays: decide whether order matters

By default, any correct comparison treats arrays as ordered. That is right for sequences such as steps or ranked results. It produces noise when an array is really a set, such as a list of tags or permissions. In that case, sort the arrays before comparing, or use a tool that can match array elements by an identifying field such as id. Check the tool's documentation for this option rather than assuming it exists.

Illustrative example

The two documents below are made up for illustration. The expected results described afterward follow from the rules above. They are not output observed from any specific tool, and exact formatting of real tool output will vary.

// Document A
{"id": 42, "name": "Ada", "tags": ["admin", "dev"], "active": true}

// Document B
{
  "name": "Ada",
  "id": 42,
  "tags": ["dev", "admin"],
  "active": 1
}
  • Plain text diff: almost every line differs, because A is on one line and B is indented with keys in a different order.
  • Normalized text diff (keys sorted, same indentation): only the tags lines and the active line differ.
  • Structural diff: reports /tags/0 and /tags/1 as changed, and /active as changed from boolean true to number 1.
  • JSON Merge Patch: would replace the whole tags array instead of describing the swap.

Comparing locally from the command line or in code

jq's -S (sort keys) option re-indents a document and sorts object keys. You can then compare the results with a standard diff tool. The example below uses bash process substitution.

diff <(jq -S . a.json) <(jq -S . b.json)

In Python, parsing both files and comparing the results ignores key order and formatting. Lists are still compared in order.

import json

with open("a.json", encoding="utf-8") as f:
    a = json.load(f)
with open("b.json", encoding="utf-8") as f:
    b = json.load(f)

print(a == b)

Python's equality has a type gap. Booleans are a subtype of integers, so True == 1 and 1 == 1.0 both evaluate as true. As a result, {"active": true} and {"active": 1} compare as equal. If type differences matter, use a comparison that checks types explicitly, or compare normalized text output instead, where true and 1 remain distinct.

Will the result stay readable and usable where you need it?

Copying and pasting JSON

A comparison is only as good as its input. Pasting JSON through other software can change it before it reaches the comparison tool. Word processors and some messaging apps can replace straight quotes with curly typographic quotes when autoformatting is enabled, which makes the JSON invalid. Copying between systems can also turn LF line endings into CRLF, or add a byte order mark at the start of a file. A text diff reports these changes on every line, while a structural diff ignores them once parsing succeeds. When results look strange, compare files directly instead of pasted text, and check that both documents parse.

Accessibility of diff output

Many diff views mark changes only with red and green highlighting. WCAG guidance (Success Criterion 1.4.1, Use of Color) says color should not be the only way to convey information. Prefer output that also uses text markers, such as + and - prefixes, or a list of changed paths. Path-based reports, including JSON Patch output, can be read line by line with a screen reader without depending on visual side-by-side alignment. Support varies by tool and assistive technology, so check with the setup you actually use.

Sensitive data and online comparison tools

JSON from APIs and configuration files often contains access tokens, passwords, personal data or internal identifiers. Pasting it into a web page gives that data to whatever the page does with it. Without the tool's documentation or your own inspection, you cannot confirm whether processing stays in your browser or the data is sent to a server and stored. Practical options:

  • Compare sensitive documents with local tools such as jq, a code editor's built-in diff, or a short script.
  • Redact secrets and personal fields before using an online tool, replacing them with placeholder values of the same type.
  • Read the tool's privacy and processing documentation. If you can, watch your browser's network activity while the comparison runs.
  • Follow your organization's rules for handling production or customer data.

Checklist: is this comparison result fit for purpose?

  • Did both documents parse as valid JSON before comparing?
  • Does the method match your goal: exact text, or data only?
  • Are arrays compared in order, and is that correct for this data?
  • Could number precision, duplicate keys or type coercion hide a real difference?
  • Could invisible or differently normalized Unicode characters explain an unexplained change?
  • Is the output readable without relying on color alone?
  • Was sensitive data kept local or redacted?

Gaps in this guide

This guide is based on the JSON specifications and on documented behavior of jq and Python. No specific online comparison service was tested, so it makes no claims about any service's features, accuracy, speed or data handling. Check those details in each tool's own documentation, or test the tool with non-sensitive sample data before relying on it.