{ }JSON Compare

Key order · canonical comparison

Compare JSON Ignoring Key Order

Two JSON files that look different are often the same document. Here is what the specification says about key order, and how to compare without tripping over it.

The JSON spec does not promise an order

A JSON object is defined as an unordered collection of name and value pairs. That single sentence in the specification (RFC 8259, section 4) is the reason two files can look different and be the same document:

{"id": 42, "name": "Ada", "active": true}
{"active": true, "name": "Ada", "id": 42}

These are equal. Not "equal enough", not "equal after normalization" — equal by the definition of the type. There is no first key, no last key, and no order to compare.

So why do the files look different?

Because every serializer has to pick some order when it writes an object, and they pick differently:

  • JavaScript keeps insertion order for string keys, so JSON.stringify writes whatever order the object was built in.
  • Python dictionaries preserve insertion order too, and json.dump writes them in that order unless you pass sort_keys=True.
  • Go's encoding/json sorts map keys alphabetically when marshalling a map, which is why two Go services rarely produce key-order noise against each other and frequently do against a Node service.
  • Databases and API gateways often preserve whatever order the row was serialized in, which can differ per record.

None of these behaviours is wrong. A reordered document is a valid serialization of the same value, and a byte-level comparison of two of them is comparing something the format never promised to fix.

When key order really does matter

Ignoring order is the right default for comparing data. It is the wrong default in three specific cases, and you should know which one you are in:

Signatures and checksums. If a hash is computed over the exact bytes of a document, then a reordered document produces a different hash and the signature fails. This is why signed JSON has to be canonicalized first; RFC 8785 (JSON Canonicalization Scheme) exists precisely to define one byte order that everyone can agree on. When you are debugging a signature failure, turn the ignore-order switch off and look at the serialized order.

Snapshot tests. If a test asserts on a stored file, and your code happens to produce keys in a stable order, a reordered output may be an early warning that something changed in the build. Some teams treat that noise as a signal; most eventually normalize the snapshot and stop caring.

Comparing two serializers. If the question is "does library A produce the same output as library B", order is part of the answer. That is a narrow question, and turning the switch off is the only way to answer it.

For everything else — API responses, config files, database exports, test fixtures, data pipelines — a key is a name and order is an implementation detail. Comparing it produces alarms that nobody can act on.

How to compare two JSON documents ignoring key order

In the browser. The JSON Compare tool has "Ignore key order" switched on by default. It sorts the keys of both documents at every level, then walks them as data, so a document whose keys were shuffled produces the answer "no differences" instead of a page of changes. If you want the sorted form for another tool, copying the documents into JSON Diff with the same switch on gives you the canonicalized text as a side effect.

With jq. jq --sort-keys . file.json prints the document with keys sorted at every nesting level. Sort both sides, then diff them:

jq --sort-keys . a.json > a.sorted.json
jq --sort-keys . b.json > b.sorted.json
diff -u a.sorted.json b.sorted.json

With Python. json.dumps(obj, sort_keys=True, indent=2) gives you the same canonical ordering in memory, which you can then compare with ==:

import json

with open("a.json") as f:
    a = json.load(f)
with open("b.json") as f:
    b = json.load(f)

print(json.dumps(a, sort_keys=True) == json.dumps(b, sort_keys=True))

This compares serialized strings, so it also catches number formatting differences that an object comparison would not. If you only want a truth value for the data, compare the loaded objects directly: Python's == on dicts ignores insertion order.

In JavaScript. Two objects with the same keys in different orders satisfy a deep equality check. assert.deepStrictEqual(a, b) from node:assert/strict passes; comparing JSON.stringify(a) === JSON.stringify(b) fails, which is the exact bug this page is about.

The related trap: array order is different

Everything above is about object keys. Arrays are a different matter: [1, 2, 3] and [3, 2, 1] are genuinely different JSON values. Most comparison tools, including this one, compare arrays by position, so a reordered array shows up as changes — correctly.

If your arrays are sets in disguise (tags, IDs, unordered result lists), sort them before comparing, or use a comparison mode that treats lists as unordered. That is a separate switch, and it changes what the result means, so it should be a deliberate choice rather than a default.

Frequently asked questions

Does key order matter in a JSON object?

No. The JSON specification defines an object as an unordered collection of name/value pairs, so {"a":1,"b":2} and {"b":2,"a":1} are the same document. Differences in key order are a property of the serializer, not of the data.

How do I compare two JSON files while ignoring key order?

The ignore-key-order switch is on by default in the JSON Compare tool. It sorts keys at every level of both documents before walking them, so a reordered object produces "no differences". Turn it off when you need to see that the serialized order changed, for example when debugging a signature.

When does key order actually matter?

When a hash or signature is computed over the exact bytes. A reordered document hashes differently, so signed JSON is canonicalized first; RFC 8785 defines the byte order used for that. Snapshot tests and serializer comparisons are the other two cases where order is part of the answer.

Does the tool also ignore the order of array elements?

Yes, and that is correct: array order is meaningful in JSON, unlike object key order. [1,2,3] and [3,2,1] are different values. If your array is really a set, sort it before comparing.

Why did two files show as different when nothing changed?

Two JSON documents with the same keys in a different order are identical as data but different as text. A deep equality check passes, a string comparison fails. Storing a stringified object and comparing strings later is the usual way this bug gets into a test suite.

How do I normalize JSON before a text diff?

Sort both sides first: jq --sort-keys, or json.dumps with sort_keys=True in Python, or an Object.keys sort before comparing. Any of them turns an order-induced diff into a real one, or into nothing at all.