JSON Sorter

Sort JSON keys alphabetically at every level.

Local Your input is processed locally in your browser. Found a problem? Report an issue

What the JSON Sorter does

The JSON Sorter reorders object keys alphabetically at every level of nesting while leaving array order untouched. Sorted keys make diffs between versions much smaller and make it easy to spot missing fields.

Using the JSON Sorter

  • Choose two spaces, four spaces or a tab per level from the Indent dropdown.

JSON Sorter examples

The built-in sample

Input

{"name":"Ada Lovelace","active":true,"tags":["math","code"],"address":{"city":"London"}}

Output

{
  "active": true,
  "address": {
    "city": "London"
  },
  "name": "Ada Lovelace",
  "tags": [
    "math",
    "code"
  ]
}

This is the input that the Load sample button fills in.

Sort order in detail

Input

{"b":1,"a":{"d":1,"c":2},"B":0,"10":1,"9":2,"list":[{"z":1,"y":2},3,1]}

Output

{
  "9": 2,
  "10": 1,
  "B": 0,
  "a": {
    "c": 2,
    "d": 1
  },
  "b": 1,
  "list": [
    {
      "y": 2,
      "z": 1
    },
    3,
    1
  ]
}

Keys that are whole numbers come first in numeric order (9 before 10), then the others by character code, so "B" sorts before "a". The array keeps its order 3, 1, but the object inside it is sorted.

Each output was produced by running this tool on the input above it.

How it works

The JSON is parsed, then every object is rebuilt with its keys in sorted order, recursively at every depth, and the result is written with the indentation you chose.

Details and limits

  • Sorting is by UTF-16 code unit, not by locale: all upper-case ASCII letters come before all lower-case ones, and accented letters come after z.
  • Arrays are never reordered, because their order usually carries meaning. Objects inside arrays are sorted.
  • Two documents that differ only in key order produce identical output, which makes the result a stable input for a text diff or a hash.
  • Input that is not valid JSON stops the tool, and the status line shows where the problem is. The JSON Validator page lists the usual causes.

Questions

Why are numeric keys always first?

JavaScript objects list keys that are whole numbers before all other keys, in ascending order, whatever order they were added in. The sorter runs in JavaScript, so its output follows that rule.