AZ Tools

JSON Sort Keys

Developer

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.

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.

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