Skip to content
LocalOnly

JSON Sort Keys

Stable

Recursively sort object keys alphabetically for stable, diffable JSON.

Everything is processed locally in your browser

About JSON Sort Keys

The JSON Sort Keys tool reorders the keys of every object in your document alphabetically, all the way down through nested structures. Consistent key order makes two JSON files diff cleanly, snapshots stay stable, and version control noise disappears. Array element order is left untouched because arrays are ordered by design.

Features

  • Recursively sorts keys in every nested object
  • Ascending (A to Z) or descending (Z to A) ordering
  • Leaves array element order intact, since arrays are inherently ordered
  • Optional case-insensitive sorting for mixed-case keys
  • Produces deterministic output ideal for diffing and snapshots
  • Preserves all values, types, and duplicate-free structure

How to use JSON Sort Keys

  1. 1

    Paste your JSON

    Add the object or array whose keys you want to normalize.

  2. 2

    Pick a direction

    Choose ascending or descending, and case-sensitive or case-insensitive ordering.

  3. 3

    Sort

    Keys are reordered recursively while values and array order stay the same.

  4. 4

    Copy the canonical output

    Use the sorted JSON for stable diffs, commits, or golden-file tests.

Examples

Unsorted to alphabetically sorted keys

Input

{
  "name": "Alan",
  "age": 41,
  "address": { "zip": "94016", "city": "SF" }
}

Output

{
  "address": {
    "city": "SF",
    "zip": "94016"
  },
  "age": 41,
  "name": "Alan"
}

How sorting JSON keys works

Key order carries no meaning - but it carries a lot of noise

RFC 8259 is explicit that a JSON object is an unordered collection of name/value pairs. Two documents whose keys appear in different orders are the same document as far as the specification is concerned, and any correct consumer must treat them identically.

In practice, though, every mainstream parser preserves insertion order, and `JSON.stringify` writes keys back in that order. So the ordering survives round trips even though nothing depends on it - which means an unrelated change upstream, a different library version, or a map iteration order can reshuffle a file and produce a diff with dozens of changed lines and zero changed data.

Sorting keys removes that noise permanently. Once every document is written in the same canonical order, a diff shows only what actually changed. This is the entire reason the operation exists.

How the sort is applied

Sorting is recursive: every object at every depth is reordered, not just the top level. That is what makes the result canonical - a partially sorted document would still diff noisily whenever a nested object shifted.

Array order is never touched, and this distinction matters. An object is unordered by definition, so reordering its keys is information-preserving. An array is ordered by definition, so reordering its elements would change the data. A tool that sorted arrays too would be silently corrupting documents, which is why this one does not.

The comparison itself is lexicographic on the UTF-16 code units of each key, matching JavaScript's default string sort. That is stable and predictable, but it is not the same as human alphabetical order: uppercase letters sort before all lowercase letters, so `Zebra` comes before `apple`. Digits sort before letters, and `item10` sorts before `item2` because the comparison is character by character rather than numeric.

Sorting as a step towards canonical JSON

Sorted keys are one part of what people mean by canonical JSON - a single, deterministic byte representation for a given value. The other parts are consistent whitespace, consistent number formatting, and consistent string escaping. Combine sorting with minifying and you have something close enough to canonical form for most practical purposes.

This matters when you hash or sign a payload. If two systems serialise the same data with different key orders, they produce different bytes and therefore different hashes, and a signature check fails even though nothing is wrong. Canonicalising before hashing is the standard fix. If you need this for cryptographic verification rather than for diffing, look at RFC 8785 (JSON Canonicalization Scheme), which pins down the number and string rules that a sort alone leaves open.

Reference

How lexicographic sorting orders keys

The comparison runs on UTF-16 code units, which is predictable but not the same as human alphabetical order.

Keys as writtenSorted resultWhy
Zebra, appleZebra, appleAll uppercase letters precede all lowercase
item10, item2item10, item2Character-by-character: '1' precedes '2'
_id, id_id, idUnderscore (U+005F) precedes lowercase letters
10, 2, 11, 10, 2Numeric-looking keys are still compared as text
éclair, zebrazebra, éclairAccented characters sit above ASCII in code point order

Which tool should you use?

These tasks overlap. Here is how to pick the right one for what you are actually doing.

Your diffs are full of moved lines that changed nothing
Sort both documents first. This is the problem the tool was built for and it usually reduces a diff to a couple of real lines.
You are comparing two API responses
Use the JSON Compare tool instead - it matches by key rather than by position, so it is already immune to ordering differences.
You need a stable input for hashing or signing
Sort, then minify. For cryptographic use, follow RFC 8785 rather than relying on a sort alone.
You want a specific field to stay at the top, like `id` or `$schema`
Alphabetical sorting cannot express that. Keep a hand-ordered file as your source of truth and sort only the copies you diff.

Use cases

  • Making two exported JSON files diff cleanly regardless of original key order
  • Producing canonical JSON before hashing or signing
  • Stabilizing test snapshots so unrelated reorderings do not fail CI
  • Normalizing config files so reviews focus on real changes
  • Comparing objects from different serializers that emit keys in different orders

Troubleshooting common errors

Array elements did not get sorted

Why: Deliberate. Arrays are ordered by definition, so reordering them would change the meaning of the document.

Fix: If you genuinely need array contents in a canonical order, sort them in code by a stable key before serialising.

Uppercase keys all cluster before lowercase ones

Why: Code-point ordering places A-Z (65-90) entirely before a-z (97-122).

Fix: This is expected and, importantly, consistent - which is all that canonical ordering requires. Normalise key casing upstream if the grouping bothers you.

`$schema` or `id` is no longer the first key

Why: Sorting is unconditional; it has no notion of privileged keys.

Fix: Sort only the copies you are diffing, and leave the authored file in its human-friendly order. Nothing that reads JSON depends on key position.

Two documents still differ after sorting

Why: The remaining differences are real - or they are formatting differences in numbers and whitespace that sorting does not address.

Fix: Minify both after sorting to eliminate whitespace and number-spelling variation, then compare again.

Limitations

What this tool deliberately does not do, so you know when to reach for something else.

  • Arrays are left in their original order by design, since array position is meaningful.
  • Sorting is lexicographic by code point, not natural or locale-aware, so `item10` precedes `item2`.
  • There is no way to pin specific keys to the top of an object.
  • Sorting alone does not produce cryptographically canonical JSON - number and string forms also need pinning per RFC 8785.
  • Duplicate keys collapse during the parse, before sorting happens.

Frequently asked questions

Learn more

Command Palette

Search for a tool or command