Skip to content

JSON Sorter

Sort JSON keys alphabetically, A to Z or Z to A, in every nested object. Choose case-sensitive, case-insensitive or natural order, and sort lists by value or by a field when you need to.

Sort keys · nested objects · lists by value or field

Valid JSON · object with 5 keys · 3 levels deep · 280 bytes

Keys
Lists
{
  "Version": "2.1.0",
  "admins": [
    {
      "id": 3,
      "name": "Sam"
    },
    {
      "id": 1,
      "name": "Ava"
    }
  ],
  "database": {
    "primary": "db-1",
    "replica10": "db-10",
    "replica2": "db-2"
  },
  "features": [
    "search",
    "cart",
    "accounts"
  ],
  "server": {
    "host": "0.0.0.0",
    "port": 8080
  }
}

14 keys moved in 5 objects. Lists keep their order unless you choose to sort them.

Everything runs in your browser; nothing is uploaded.

Why sort JSON keys?

  • Compare two files. Two JSON files with the same data but a different key order look completely different in a diff. Sort both the same way and only the real changes show.
  • Cleaner version control. Config and translation files that are always saved sorted give small, readable Git diffs and fewer merge conflicts.
  • Find keys faster. In a long object, alphabetical order means you know where to look.
  • Stable tests and hashes. Snapshot tests, caches and signatures need the same output every time, whatever order the code built the object in.

Sorting keys never changes the data: the JSON standard treats an object as an unordered set of names and values. List order does matter, so ["first", "second"] is left alone unless you ask to sort it.

How to sort JSON online

  1. Paste your JSON or open a file. Mistakes such as trailing commas are pointed out, with a one-click fix.
  2. Pick the order: A → Z or Z → A, and case-sensitive, ignore case or natural.
  3. Decide about lists. Leave them as they are (the default), sort plain values, or sort lists of objects by a field such as id or name.
  4. Copy or download the sorted JSON, indented or minified.

The result updates as you change options. Long numbers keep every digit, and number-like keys such as "10" are sorted like any other key.

Sort options explained

JSON sorter options, what they do and an example of each
OptionWhat it doesExample
A → Z / Z → AAscending or descending key ordera, b, c / c, b, a
Case-sensitiveCharacter-code order: uppercase before lowercase, digits first. Same as jq -S and PythonZebra, apple, banana
Ignore caseDictionary order, like VS Code’s Sort Documentapple, banana, Zebra
NaturalNumbers inside keys sort by valueitem2, item10
Nested objectsSorts every object at every level; off sorts the top level onlyon by default
Sort plain valuesSorts lists of numbers, text, true/false and null[10, 9, 1] → [1, 9, 10]
Sort lists of objects byOrders objects by one field; items without it go lastby id: 1, 2, 3

Numbers in a list are sorted by value, not as text, so 9 comes before 10. Mixed lists go null, then false/true, then numbers, then text.

Example: sort a config file

This config (the example in the tool) was written by hand, in no particular order:

{ "server": { "port": 8080, "host": "0.0.0.0" }, "Version": "2.1.0", "features": ["search", "cart", "accounts"], "database": { "replica10": "db-10", "replica2": "db-2", "primary": "db-1" }, "admins": [ { "name": "Sam", "id": 3 }, { "name": "Ava", "id": 1 } ] }

Step 1: A → Z, case-sensitive

{ "Version": "2.1.0", "admins": [ { "id": 3, "name": "Sam" }, { "id": 1, "name": "Ava" } ], "database": { "primary": "db-1", "replica10": "db-10", "replica2": "db-2" }, "features": [ "search", "cart", "accounts" ], "server": { "host": "0.0.0.0", "port": 8080 } }

Every object is sorted, including the ones inside admins. Two things look odd: Version comes first, because uppercase letters come before lowercase in character order, and replica10 comes before replica2, because 1 comes before 2.

Step 2: Switch to Natural

{ "admins": [ { "id": 3, "name": "Sam" }, { "id": 1, "name": "Ava" } ], "database": { "primary": "db-1", "replica2": "db-2", "replica10": "db-10" }, "features": [ "search", "cart", "accounts" ], "server": { "host": "0.0.0.0", "port": 8080 }, "Version": "2.1.0" }

Now it reads the way a person would sort it: Version sorts under V, after server, and replica2 comes before replica10. The features list kept its order, because list order can matter.

How to sort JSON in code and editors

# Python: sorts nested objects too
json.dumps(data, sort_keys=True, indent=2)

# Command line, with jq
jq -S . config.json > sorted.json

// JavaScript: sort keys at every level
function sortKeys(value) {
  if (Array.isArray(value)) return value.map(sortKeys);
  if (value && typeof value === "object") {
    return Object.fromEntries(
      Object.keys(value).sort().map((key) => [key, sortKeys(value[key])])
    );
  }
  return value;
}

// Sort a list of objects by a field
items.sort((a, b) => a.id - b.id);                        // numbers
items.sort((a, b) => a.name.localeCompare(b.name));       // text
sorted(items, key=lambda item: item["id"])                # Python

In VS Code, open the Command Palette (Ctrl+Shift+P, or Cmd+Shift+P on a Mac) in a JSON file and run JSON: Sort Document. It sorts nested objects too, ignoring case.

Two JavaScript traps:

  • [10, 9, 1, 100].sort() gives [1, 10, 100, 9], because it compares numbers as text. Pass (a, b) => a - b.
  • Objects always list integer-like keys first. However you sort {"b", "10", "2"}, the object comes out "2", "10", "b". The tool above doesn’t have this problem.

Frequently asked questions

Does sorting JSON keys change the data?

No. JSON objects are unordered, so any reader gets the same values whatever the key order. Sorting lists is different: list order can carry meaning (steps, rankings), which is why lists are only sorted when you ask.

Why do uppercase keys come before lowercase ones?

Case-sensitive sorting uses character codes, where A–Z (65–90) come before a–z (97–122). That is what jq -S and Python’s sort_keys do. Choose Ignore case for dictionary order.

How do I sort a JSON array by a value?

Paste the JSON and choose the field under Sort lists of objects by, for example price or name. Numbers sort by value, text alphabetically, and items without that field go to the end.

Why does key10 come before key2?

Alphabetical order compares one character at a time, and 1 comes before 2. Natural order reads the whole number, so key2 comes before key10.

Sources