Skip to tool
ecech.
💻 Developer & Code

Sort JSON Keys So the Diff Shows What Changed, Not What Moved

Key order means nothing to a parser and everything to git. Sorting recursively turns a reshuffled file back into a one-line diff.

Keys sorted

Objects touched

Lines that moved

diff noise removed

Max depth

Sorted



Notes

Advertisement

How the calculation works

One dependency added, two diffs unsorted - "react": "^18.0.0", + "axios": "^1.6.0", + "react": "^18.0.0", - "lodash": "^4.17.21" which line is the change? sorted   "axios": "^1.6.0",   "lodash": "^4.17.21", + "react": "^18.0.0" one line, obviously

How to Use This Tool

Paste JSON and it comes back with every object's keys sorted, recursively. Arrays keep their order unless you ask otherwise, because array order is data.

Key order means nothing, until git looks at it

The JSON specification says an object is an unordered set of name-value pairs. Every parser agrees: two documents with the same keys in a different order parse to the same value.

Git does not parse JSON. It compares lines. So a tool that serialises keys in insertion order, or a language whose map iteration order varies between runs, produces diffs where most of the changed lines simply moved. The real change hides among them, and reviewers stop reading carefully — which is the actual cost.

Sorting keys before committing makes the diff show what changed. It is why lockfiles are sorted, why jq has --sort-keys, and why a lot of CI pipelines normalise config files before comparing them.

Arrays are not sorted, and should not be

An object's keys are unordered by definition. An array's elements are ordered by definition, and the order is usually meaningful: a list of middleware runs in sequence, a list of steps happens in order, a list of results came back ranked.

Sorting an array therefore changes what the document means, which is why it is off by default. The option exists for arrays that genuinely are sets — a list of tags, a list of allowed hosts — and it only touches arrays containing nothing but strings, since those are the ones that are usually sets.

Duplicate keys are legal and lossy {"a": 1, "a": 2} {"a": 2} the first value is gone The spec allows duplicates, parsers keep the last, and re-serialising discards the rest without a warning. Worth knowing before you sort a file that was hand-edited or merged badly.
This tool counts duplicates in your input and tells you what was lost.

Duplicate keys disappear

JSON permits duplicate keys and almost every parser keeps the last one. So {"a":1,"a":2} parses to {"a":2}, and re-serialising loses the first value with no error.

That matters when a file has been hand-edited or badly merged, because the duplicate is often the symptom you were trying to find. This tool counts them in your input and reports them before they are gone.

Which ordering to choose

  • Natural — treats digit runs as numbers, so item2 comes before item10. Best for anything a human reads.
  • Strict — compares by code point, which is what jq --sort-keys and most language sort functions do. Choose this if another tool in your pipeline will sort the same file, so the two agree.
  • Case-insensitive — groups Name with name. Readable, and not reproducible across tools, so avoid it for anything committed.

If the point is reproducible diffs, consistency matters more than which order you pick — and strict is the one other tools will agree with.

Advertisement

Frequently Asked Questions

Does the order of keys matter in JSON?
Not to any parser — the specification defines an object as an unordered set of name-value pairs. It matters enormously to git, which compares lines and cannot tell a moved line from a changed one. That is the whole reason to sort.
Why does my JSON diff show so many changed lines?
Because something re-serialised the file with keys in a different order. Every moved line shows as a deletion and an addition, so a one-field change can touch half the file and the real change hides among the noise.
Should I sort arrays too?
Usually not. Object keys are unordered by definition; array elements are ordered by definition and the order is normally meaningful — middleware runs in sequence, results come back ranked. Sort arrays only when they are genuinely sets, like a list of tags.
What happens to duplicate keys?
They collapse. JSON allows duplicates and parsers keep the last one, so re-serialising discards the others with no error. This matters most on hand-edited or badly merged files, where the duplicate is often the bug you were looking for — so it is reported before sorting.
Which ordering should I use?
Strict code-point order if anything else in your pipeline will sort the same file, because that is what jq and most language sort functions do and the two will then agree. Natural ordering reads better for humans but other tools will not reproduce it.
Does sorting change what the JSON means?
No, provided arrays are left alone. Reordering object keys produces a document that every parser treats as identical. Sorting arrays does change meaning, which is why it is a separate option and off by default.

Related tools in Developer & Code

Browse all Developer & Code tools
The Mac mini the ecech. site is built on, beside a handwritten note reading ecech.com.

Made by one person

ecech. is not a content farm. Every tool here is written and checked by hand, one at a time, by someone who wanted the tool to exist and could not find a version that showed its working.

No accounts and no sign-in, and nothing you type reaches a server — every calculation on this page runs inside your browser. The ads are served by Google and do set their own cookies, which is set out in full on the privacy page. More about the site.