Skip to content

Sort JSON Keys

Sort JSON keys alphabetically at every depth in one click. Array items stay where they are.

Input · JSON
object {5}
"welcome": "Welcome back"
"signOut": "Sign out"
"cart": {3}
"title": "Your cart"
"empty": "Nothing here yet"
"checkout": "Check out"
"account": "Account"
"errors": {2}
"network": "Check your connection"
"auth": "Please sign in again"
Valid JSON5 top-level keys · depth 3 · 10 keysRuns in your browser

Load from a URL

Your browser fetches it directly, so the server must allow cross-origin requests (CORS).

How to sort JSON keys

  1. Paste JSON or press Open file.
  2. Press Sort keys or Ctrl + Enter. The result uses your Indent setting.

Example

A config in no particular order:

{"timeout": 30, "Retries": 2, "endpoints": {"primary": "https://api.example.com", "backup": "https://api-eu.example.com"}, "allowedOrigins": ["https://shop.example", "https://admin.example"], "_comment": "staging", "2fa": true, "item10": 1, "item2": 2}

After Sort keys:

{
  "2fa": true,
  "Retries": 2,
  "_comment": "staging",
  "allowedOrigins": [
    "https://shop.example",
    "https://admin.example"
  ],
  "endpoints": {
    "backup": "https://api-eu.example.com",
    "primary": "https://api.example.com"
  },
  "item10": 1,
  "item2": 2,
  "timeout": 30
}

endpoints is sorted inside too, while allowedOrigins keeps shop before admin: array order carries meaning (steps, rankings), so items never move. Objects inside arrays still get their keys sorted.

The sort order

Keys are compared by Unicode value, character by character, as JavaScript sorts strings by default:

  • digits before letters: 2fa is first
  • uppercase before lowercase: Retries before allowedOrigins
  • _ between the two cases
  • no number reading: item10 before item2

Python’s json.dumps(data, sort_keys=True) gives the same order for everyday keys, so a Python script will not reorder a file sorted here.

Keys that look like numbers

"10" sorts before "2" here because both are text. Load the result into a JavaScript object and stringify it, and JavaScript moves integer-like keys to the front in numeric order, "2" before "10". Python keeps the order it reads.

Why sort keys

Two exports of the same data often order keys differently, which makes git diffs noisy; once both are sorted, only real changes remain. JSON diff can also ignore key order by itself. Sorting also puts duplicate keys side by side, and the JSON validator flags them.

Skip it where people expect a set order, such as name and version at the top of a package.json. For indentation without reordering, use JSON pretty print.

Questions

Does sorting the keys change the data?

No. RFC 8259 defines an object as an unordered collection of name/value pairs, so parsers read the sorted and the unsorted versions as the same data.

Can I sort in reverse or ignore case?

No. Keys are always sorted from A to Z by character code.

How do I sort an array of objects by a field?

This tool never reorders arrays. To browse the items in a different order, open the JSON to table view and click a column header.

How do I sort JSON keys on the command line?

With Python: python -m json.tool --sort-keys data.json. It sorts at every level and indents with 4 spaces.

Related tools

Esc