DevTools Logo

JSON Key Sorter

JSON Key Sorter

Deterministic key order for clean diffs, stable snapshots and canonical output.

Runs in your browser
Try an example

JSON input

Sorted JSON

No JSON yet

Paste a document or load an example to sort its keys.

Examples

Custom priority order

Input
{"x":1,"name":"n","id":7,"z":2}
Output
{"id":7,"name":"n","x":1,"z":2}

The priority list id, name, * places those two keys first and sorts everything else alphabetically.

Array sorting

Input
{"items":[{"n":3},{"n":1},{}]}
Output
{"items":[{"n":1},{"n":3},{}]}

Every element is an object, so sorting by n is safe; the object without n remains last.

Large integers

Input
{"id":12345678901234567890,"b":1}
Output
{"b":1,"id":12345678901234567890}

A BigInt path keeps the 19-digit value exact while keys are reordered.

About this tool

The JSON Key Sorter reorders object keys without touching values. A small hand-written reader keeps entries in an ordered Map instead of a plain object, so integer-like keys such as "10" and "9" appear in true sorted order rather than being hoisted by JavaScript's property rules. Integers too large for IEEE-754 precision are read as BigInt and written back as bare digits, so a 19-digit identifier survives the round trip.

Three sort modes cover the common workflows: ascending code-point order (the deterministic default), descending order, and a custom priority list such as id, name, * where the star marks where the remaining alphabetically sorted keys go. The comparator can be switched to locale-aware collation and case-insensitive matching when human-facing order matters more than byte stability.

Arrays of objects can be sorted by any detected field, ascending or descending. Sorts only run when every element is an object; a mixed array is left alone rather than partially reordered, and items missing the chosen field always stay at the end. The summary reports how many keys moved, how deep the walker went, how many arrays were reordered, and any duplicate keys found on input.

Output can be pretty-printed, minified, or canonical (minified with sorted keys), ready to paste into a diff, a snapshot fixture, or a hashing pipeline. Everything runs locally: no upload, no account, no storage.

What this tool does not do

  • Canonical output defines key order and separators only; it is not a full RFC 8785 implementation (number formatting follows JavaScript values).
  • Duplicate keys are resolved last-wins with a warning, matching JSON.parse behavior in every runtime.
  • Locale-aware collation can differ between browsers; use code-point mode when byte-identical output matters.
  • Very large documents are parsed on the main thread; keep inputs to a few megabytes for a responsive tab.

How to use this tool

  1. Paste JSON

    Paste or type a document, or load one of the example chips — including an invalid one that demonstrates the error state.

  2. Choose an order

    Pick ascending, descending or custom order. In custom mode write comma-separated keys and use * for the remainder.

  3. Tune the walk

    Toggle recursive sorting, comparator mode, case sensitivity and optional array sorting by a detected field.

  4. Copy stable output

    Read the moved-key summary, choose pretty, minified or canonical output, then copy or download the result.

Use cases

Cleaner code review diffs

Normalize config or state files before committing so reviewers see the real change instead of key shuffles.

Stable snapshot fixtures

Give CI the same byte sequence every run by making key order deterministic before writing snapshots or hashes.

Canonical hash input

Produce sorted, minified JSON with numeric-like keys in the right place as input to a signing or checksum pipeline.

Sort modes and output shapes

SettingEffect
ascendingCode-point order: uppercase before lowercase, deterministic across machines.
descendingReverse of the chosen comparator.
custom: id, name, *Priority keys first, then the remaining keys alphabetically at the star position.
locale comparatorIntl.Collator ordering for human-facing keys; code-point is the default.
pretty / minified / canonicalTwo-space indentation, no whitespace, or minified with sorted keys for hashing.
array fieldSort arrays of objects by a detected field; missing values last, mixed arrays skipped.

Values are never reordered or rewritten; only key and array position change.

Common mistakes

Mistake:Expecting whitespace formatting from a key sorter.

Fix:This tool changes order, not indentation. Use a JSON formatter for pretty-printing; the Output format control only selects compact vs indented emission.

Mistake:Assuming JavaScript object order matches the sorted output for numeric keys.

Fix:The built-in sorter walks an ordered Map and emits keys explicitly, so "10" correctly precedes "9" in code-point mode.

Mistake:Sorting an array whose elements mix objects and scalars.

Fix:Array sorting is skipped entirely in that case, with a notice in the summary — no partial reorder.

Frequently asked questions

Does sorting keys change my JSON?

No values are changed — only the position of keys and, if you enable it, the order of arrays of objects. Number tokens are normalized by parsing (1.50 becomes 1.5), while integers beyond the safe range are handled as BigInt and written back exactly. The summary tells you when that path was used and lists any duplicate keys that were resolved last-wins.

How do I keep certain keys first?

Choose Custom order and enter a comma-separated list such as id, name, *. Listed keys come first in that order; the star marks where all remaining keys go, alphabetically. If you omit the star, the unlisted keys are appended alphabetically at the end.

Can it sort arrays too?

Yes. Turn on array sorting and pick any field detected in your data. Every array element must be an object; if a scalar appears anywhere in the array, sorting is skipped with a notice. Items missing the chosen field always stay last, including in descending mode.

Is the output valid for hashing?

Canonical mode emits sorted keys with no whitespace, which gives a stable byte sequence for ordinary JSON values. It is not full RFC 8785 canonicalization: number formatting follows JavaScript semantics, so review before using it for signature schemes that demand strict JCS number rules.

What happens to duplicate keys?

The last occurrence wins, just like JSON.parse in every browser and Node. The summary lists every duplicated key name so you can fix the source rather than silently losing data.

Is my JSON uploaded anywhere?

No. Parsing, sorting, duplicate detection and emission all run in this browser tab. There is no server request, no account and no stored copy of your document.

References & standards