Compare JSON lists
How to compare JSON arrays by position, as unordered collections, or by record key, and how to check that the result is accurate, readable and safe to share.
What comparing JSON lists actually checks
In JSON, a list is an array. Comparing two arrays does not change either one. It reports whether they match and, with a good tool, what differs. The answer depends on which equality rule you apply. The JSON specification (RFC 8259) defines arrays as ordered sequences and objects as unordered collections of name/value pairs, so position matters inside an array by default, but key order inside an object does not.
- Text comparison: compares the raw characters line by line. Indentation, key order, spacing and escape sequences all count as differences, even when the data is the same.
- Ordered (positional) comparison: parses both documents, then compares element 1 with element 1, element 2 with element 2, and so on.
- Unordered comparison: checks whether both arrays contain the same elements regardless of position. You must decide whether duplicates count (a multiset comparison) or not (a set comparison).
- Keyed comparison: for arrays of objects, matches records by an identifier field such as id, then compares the fields of each matched pair.
Order-insensitive comparison is easy to confuse with reversing or reordering a list, but the two are different. Reversing an array is a transformation that changes the stored data. An unordered comparison leaves the data alone and simply ignores position when judging equality. A reversed copy of an array is therefore different under positional comparison and equal under unordered comparison.
One input, four results
The following pair of arrays holds the same two records, written in a different order, with different key order and spacing.
a.json
[{"id": 1, "name": "Ada"}, {"id": 2, "name": "Lin"}]
b.json
[
{"name": "Lin", "id": 2},
{"id": 1, "name": "Ada"}
]
The results below are illustrative. They follow from each rule as defined above and were not produced by a specific tool.
| Method | Result | Reason |
| Text comparison | Different | Line breaks, spacing, key order and element order all differ |
| Ordered comparison | Different | Position 1 holds id 1 in a.json and id 2 in b.json |
| Unordered comparison | Equal | Both arrays contain the same two objects; key order inside objects is ignored after parsing |
| Keyed comparison by id | Equal | Each id appears once in both files with identical fields |
Which approach suits your result
Choose the rule that matches what the order and identity of elements mean in your data. The wrong rule produces either false alarms or missed changes.
| Approach | Use it when | Main tradeoff |
| Ordered | Order carries meaning: steps, rankings, time series, paginated results | One inserted element shifts every later position, so a small change can look like many |
| Unordered (set) | Only membership matters and duplicates are irrelevant: tags, permission names | Hides duplicate entries, which may themselves be a bug |
| Unordered (multiset) | Membership matters and duplicate counts matter | Reports that counts differ but not where; elements must be compared in a canonical form |
| Keyed | Records have a stable, unique identifier: API responses, database exports | Requires the key to be present and unique; missing or repeated keys need explicit handling |
| Text comparison after normalization | You need a human-reviewable diff, such as in version control | Normalizing key order is safe, but sorting arrays to reduce noise changes their meaning |
For most record-style data, keyed comparison gives the most useful report: which records were added, which were removed, and which changed, rather than a cascade of positional mismatches. Use ordered comparison only when position is part of the meaning, and use text comparison mainly for reviewing changes, not for deciding equality.
Running each comparison locally
These snippets show the rules in Python and jq. They are illustrative and were not executed for this article. Try them on a small known case, such as the pair above, before relying on them.
import json
from collections import Counter
with open("a.json") as f:
a = json.load(f)
with open("b.json") as f:
b = json.load(f)
# Ordered: lists compare by position; dicts ignore key order
print("ordered equal:", a == b)
# Unordered with duplicate counts: compare canonical serializations
def canon(x):
return json.dumps(x, sort_keys=True, separators=(",", ":"), ensure_ascii=False)
print("unordered equal:", Counter(map(canon, a)) == Counter(map(canon, b)))
# Keyed by "id"
ka = {item["id"]: item for item in a}
kb = {item["id"]: item for item in b}
if len(ka) != len(a) or len(kb) != len(b):
print("warning: duplicate ids were collapsed")
print("added:", sorted(kb.keys() - ka.keys()))
print("removed:", sorted(ka.keys() - kb.keys()))
print("changed:", sorted(k for k in ka.keys() & kb.keys() if ka[k] != kb[k]))
# Ordered equality (jq compares objects regardless of key order)
jq -n --slurpfile a a.json --slurpfile b b.json '$a[0] == $b[0]'
# Unordered equality, duplicates counted
jq -n --slurpfile a a.json --slurpfile b b.json '($a[0] | sort) == ($b[0] | sort)'
# Reviewable text diff with keys sorted (bash)
diff <(jq -S . a.json) <(jq -S . b.json)
Two caveats apply to the Python version. The canonical-serialization step writes 1 and 1.0 differently, so the unordered check treats them as distinct even though Python's own equality treats them as equal. And building a dictionary by id silently keeps only the last record for a repeated id, which is why the snippet checks lengths.
Will the result stay accurate and usable?
A comparison is only as trustworthy as the parsing and normalization behind it. These are the common places where two lists that look the same compare as different, or the reverse.
- Large numbers: parsers that store numbers as IEEE 754 double-precision values, including standard JavaScript parsing, cannot represent every integer above 2^53 (9007199254740992). Two distinct large IDs can round to the same value and compare as equal. Store such IDs as strings or use a parser that preserves full precision.
- Number spelling: 1, 1.0 and 1e0 are the same value in some tools and different in others. Check how your tool treats them.
- Escapes: "\u00e9" and "é" are the same string after parsing but differ in a text comparison.
- Unicode normalization: a precomposed é (U+00E9) and an e followed by a combining accent (U+0065 U+0301) look identical but are different code point sequences. Comparisons treat them as different unless both inputs are normalized to the same form, such as NFC.
- Invisible characters: zero-width spaces, non-breaking spaces and trailing whitespace inside string values make otherwise identical entries unequal.
- Duplicate object keys: RFC 8259 says names within an object should be unique but does not require it, and parsers handle duplicates differently. Many keep the last value, which means a comparison can silently ignore one of the duplicates.
- Missing versus null: decide whether a field that is absent should match a field set to null. Most equality checks treat them as different.
- Invalid input: trailing commas, comments and single quotes are not valid JSON. Strict parsers reject them, so validate both inputs before comparing.
Copying and pasting is a frequent source of these problems. Text copied from word processors, chat apps or web pages may arrive with curly quotes substituted for straight ones, which makes the JSON invalid, or with non-breaking spaces that change string values. If a comparison reports unexpected differences, validate the pasted input and inspect suspicious strings at the code point level.
Consider how the result will be read. A report that names the path of each difference, such as the price field of the record with id 42, is easier to act on than line numbers. A diff that marks additions and removals only with color fails people who cannot distinguish those colors and may not be conveyed by assistive technology; WCAG's Use of Color criterion advises against relying on color alone. Prefer output that also labels changes in text, for example with added, removed and changed headings or plus and minus markers.
Be careful with sensitive data. A browser-based comparer may send your input to a server unless its documentation clearly states that processing happens locally, and you generally cannot verify retention from the page itself. Do not paste API tokens, credentials, personal data or customer records into an online tool you have not vetted. Redact sensitive fields first, or run the comparison locally with a script or command-line tool like those above.
Gaps in this guide
No test results, benchmarks or documentation for specific JSON comparison sites or applications were available for this guide, so it does not rate or recommend any particular product. When evaluating one, check its documentation for four things: whether array order is respected or ignored and whether you can choose, how duplicates and missing keys are handled, whether large numbers keep full precision, and whether processing happens on your device or on a server.