"category": "utility",

JSON Key Sorter

"tldr": Paste JSON and get every object's keys sorted alphabetically at all depths - deterministic, diff-friendly output; arrays untouched.

Sort every object's keys alphabetically, recursively through the whole document, and pretty-print the result. Array order is preserved - only object keys are reordered, which never changes the meaning of the data.

{"json": "sorted json"}

Sorted keys make JSON deterministic: two payloads with the same data always serialize identically, so text diffs show real changes instead of key-order noise. That is why config files, lockfiles, and snapshot tests conventionally keep keys sorted.

How to sort JSON keys

  1. 1Paste your JSON into the input panel.
  2. 2Keys are sorted alphabetically at every nesting depth; arrays keep their element order.
  3. 3The result is pretty-printed with 2-space indentation.
  4. 4Copy the sorted JSON - comparing two sorted documents in our Diff tool shows only real differences.

Convert JSON to Sorted JSON in code

Python
import json

print(json.dumps(data, sort_keys=True, indent=2))
JavaScript (recursive)
function sortKeys(value) {
  if (Array.isArray(value)) return value.map(sortKeys);
  if (value && typeof value === "object") {
    return Object.fromEntries(
      Object.keys(value).sort().map(k => [k, sortKeys(value[k])])
    );
  }
  return value;
}
const sorted = JSON.stringify(sortKeys(data), null, 2);

Frequently asked questions

Does key order ever matter in JSON?

Per the specification, no - objects are unordered collections, and any compliant consumer must treat {"a":1,"b":2} and {"b":2,"a":1} identically. Sorting is always safe. (Only non-compliant consumers that parse JSON with regexes or depend on insertion order would notice - and those are bugs.)

Why are my array elements not sorted?

Array order is data - [1, 2] and [2, 1] are different values, unlike object key order. Sorting arrays would silently change meaning, so the tool never does it. Sort arrays in code where you know the sort key.

How does this help with diffs and version control?

When different tools or code paths write the same config with different key orders, every commit shows spurious changes. Sorting before saving normalizes the file so diffs show only genuine edits. Pair it with consistent indentation for fully deterministic files.

How do I sort keys in code?

Python: json.dumps(data, sort_keys=True, indent=2). JavaScript has no built-in flag - pass a sorted key array as the replacer: JSON.stringify(obj, Object.keys(flatten(obj)).sort(), 2) only handles top level, so recurse (see example) or use a library like json-stable-stringify.

Last updated:

You might also need

Free and in-browser, like everything on JSON Console. Browse all tools →