JSON Sorter

Alphabetise JSON keys recursively to make documents diffable.

Input
JSON input
Output
Result
Options

Spaces per level of nesting.

About this tool


Two JSON documents can hold identical data and still produce a noisy diff, purely because their keys are in different orders. Sorting keys alphabetically at every level of nesting gives both files a canonical layout, so a diff shows only real changes.

This is routinely useful for comparing API responses between environments, normalising config files before committing them, and reviewing lock files or generated JSON where key order is arbitrary.

How to use it

  1. Paste the first documentLoad the JSON you want to normalise.
  2. SortPress Sort keys. Nested objects are sorted at every depth.
  3. Repeat for the other documentSort both files the same way, then diff the results.
  4. Copy or downloadSave the canonical form for comparison or version control.

Worked examples


Each example below is executed against this tool by the test suite, so what you see is what the tool actually produces.

Keys alphabetised at every level

Input

{"name":"Ada","address":{"postcode":"N1","city":"London"},"age":36}

Output

{
  "address": {
    "city": "London",
    "postcode": "N1"
  },
  "age": 36,
  "name": "Ada"
}

The nested address object is sorted too, not just the top level.

What to watch for


The details that decide whether a conversion is correct, and where information can be lost without any error being raised.

Array order is preserved by default
Arrays are ordered collections, so their order is meaningful and left untouched. Sorting [3,1,2] into [1,2,3] would change what the document means. The optional "Sort arrays of scalars" switch reorders arrays that contain only strings, numbers or booleans, useful for normalising tag or permission lists where order carries no information. Arrays containing objects are never reordered.
How keys are compared
Keys are compared with locale-aware natural ordering, so item2 sorts before item10 rather than after it. That matches how people read numbered keys, though it differs from the byte ordering some tools use.
Sorting does not change meaning
JSON objects are formally unordered, so reordering keys yields an equivalent document. Be aware that some consumers do depend on the order they receive, particularly when a JSON document is used to produce a signature or checksum, sort before signing, never after.

Limitations


  • Arrays containing objects are never reordered, since no sort key can be inferred safely.
  • Sorting changes the serialised bytes, so do it before generating any signature or checksum.
  • Processing happens in your browser, so very large inputs are bounded by available memory. Files above roughly 10 MB are handled but will feel slower, and multi-hundred-megabyte files are better suited to a command-line tool.

Questions


Will sorting break my application?
Object key order is not significant in JSON, so a conforming consumer is unaffected. The exception is anything that hashes or signs the serialised text, where any reordering changes the result.
Why are my arrays not sorted?
Array order is meaningful, so it is preserved by default. Enable "Sort arrays of scalars" in Options to sort arrays that contain only primitive values.