Skip to content
AZ Tools

JSON Sort Keys

Sort the keys of a JSON object alphabetically. Optionally recurse into nested objects so the whole document is canonicalized.

Sorted

{
  "author": {
    "email": "demo@example.com",
    "name": "Alice"
  },
  "dependencies": {
    "astro": "^4",
    "react": "^18"
  },
  "name": "demo",
  "scripts": {
    "build": "astro build",
    "dev": "astro dev",
    "test": "vitest"
  },
  "version": "1.0.0"
}

Only object keys are reordered. Array element order is preserved — sort those separately if needed.

How to use

  1. Paste your JSON (a package.json or any object).
  2. Toggle deep to sort nested objects too, or leave it off to sort only top-level keys.
  3. Pick ascending or descending order and copy the result.

About this tool

Show

Reorders the keys of a JSON object in alphabetic order, with optional recursive descent into nested objects and arrays of objects. Arrays themselves are left in their original order — only object key order changes. Useful for deterministic diffs (committing a sorted package.json so reviewers see only meaningful changes), normalizing API fixtures before snapshot testing, or matching the expected order in a downstream tool that's strict about key order.

Leaving arrays alone is the correct behaviour rather than a missing feature. An object is an unordered collection by definition, so reordering its keys cannot change what it means, while a JSON array is ordered and sorting it would change the document. That is also why a sorted file and its original parse to equal values: the diff moves, the data does not.

The sort is on the string form of the key, which is not the same as an intuitive order. Uppercase letters sort before lowercase ones, so Name lands above age; digits sort before letters; and a numeric key sorts as text, which puts 10 before 2. A file with mixed-case keys will look shuffled rather than alphabetised the first time, and that is the ordering every other tool doing the same thing will produce too.

JSON permits duplicate keys and parsers handle them by keeping the last one, so a document that carries the same key twice loses a value here as it would anywhere else. Very large integers are the other quiet loss: values beyond the safe integer range change when they pass through a parse and re-serialise, which is why a 19-digit identifier should be a string.

One warning about the package.json case, which is the most common reason to reach for this. Sorting keys is safe, but the fields inside that file are not all order-free in practice: the exports map is matched in declaration order by Node's resolver, so sorting it can change which entry point a package hands out. Sort the top level and leave that one alone.

Frequently asked questions

Does this re-order array elements?

No — only object keys. Arrays keep their original order because element position is usually meaningful. If you also need array elements sorted, run a dedicated sort step (jq sort_by, for example) first.

Why would I sort package.json?

Tools like npm are tolerant of key order, but humans aren't. Sorting keeps reviewers focused on real changes (a new dependency) instead of churn from npm install reshuffling. Some teams enforce this via a sort-package-json pre-commit hook.

Is the sort locale-aware?

No — it's a code-point sort with optional case-insensitive folding. That matches what jq, Object.keys().sort(), and most language standard libraries do by default. Locale collation can give different results for non-ASCII keys, but is rarely what you want for canonical output.

What about JSON5 / comments / trailing commas?

Strict JSON only — the input must parse via JSON.parse. If you have a config file with comments, strip them first or use a JSON5 parser before pasting in.

Related tools