JSON Sorter – Sort JSON Keys

Sort the keys of a JSON object into alphabetical order at every level, including objects inside arrays. Array order is kept. Nothing leaves your device.

Your result appears here.

What the sorter does

The tool parses your JSON object, takes its list of keys, sorts that list with the default JavaScript string comparison, and rebuilds the object in the new order. It then pretty-prints the result with two-space indentation. The comparison is by UTF-16 code unit, which means it is case-sensitive: uppercase letters sort ahead of lowercase, and digits sort ahead of letters. Values are never changed. Nested objects, including objects inside arrays, are sorted the same way, and array items keep their order.

How to sort JSON keys

  1. Paste a JSON object into Input. Leading whitespace and a byte order mark are stripped automatically.
  2. Click Run tool. Keys are sorted at every level and the result is pretty-printed with two-space indentation.
  3. Click Copy result and paste it back into your file. If the input is not valid JSON, the parser message appears below the buttons instead.

Why sort keys

A deterministic key order makes JSON files behave well in version control: two people editing the same config produce a minimal diff instead of a reshuffle. It also makes two objects easy to compare by eye, helps you find a key quickly in a large object, and gives generated output a stable shape for snapshot tests. Many teams alphabetize package.json-style dependency blocks and environment dumps for exactly this reason.

Worked example

Input:

{"name": "x", "age": 3, "Active": true, "id": 42}

Output:

{
  "Active": true,
  "age": 3,
  "id": 42,
  "name": "x"
}

"Active" comes first because its capital A outranks every lowercase letter.

Sort order reference

Key groupCode point rangeSorts
Digits 0-948–57First
Uppercase A-Z65–90Second
Lowercase a-z97–122Third

Numeric keys and other edge cases

{"b": 1, "10": "ten", "9": "nine", "B": 2, "_meta": {}}

sorts to

{
  "9": "nine",
  "10": "ten",
  "B": 2,
  "_meta": {},
  "b": 1
}

Integer-like keys are always listed first in numeric order, because that is how JavaScript objects store them, so "9" precedes "10" even though a plain string sort would put "10" first. The underscore (code 95) sorts after uppercase letters and before lowercase ones.

Nested objects are sorted too, including objects inside arrays, but the order of array items never changes:

{"z": {"y": 1, "x": 2}, "a": [3, 1, {"d": 0, "c": 1}]}

becomes

{
  "a": [
    3,
    1,
    {
      "c": 1,
      "d": 0
    }
  ],
  "z": {
    "x": 2,
    "y": 1
  }
}

becomes

{
  "a": [
    3,
    1,
    2
  ],
  "z": {
    "y": 1,
    "x": 2
  }
}

How to sort a JSON array

Arrays are ordered lists, so this sorter never reorders them. When you do want to sort a JSON array, pick the key explicitly in code:

// JavaScript: numbers ascending, strings alphabetically
rows.sort((a, b) => a.price - b.price);
rows.sort((a, b) => a.name.localeCompare(b.name));

# jq
jq 'sort_by(.price)' data.json
jq 'sort_by(.name) | reverse' data.json

# Python
rows.sort(key=lambda r: r["price"])

Beware of calling sort() with no comparator in JavaScript: it compares items as strings, so [10, 9, 1] becomes [1, 10, 9].

Sorting keys at every level

This gives the same key order as jq -S . data.json or Python's json.dumps(data, sort_keys=True, indent=2). Both leave arrays in their original order. Canonical key order is what makes two JSON documents comparable with a plain text diff; for a structural comparison that ignores key order altogether, use the JSON diff checker.

Does key order matter in JSON?

By the specification, no: RFC 8259 calls an object an unordered collection, and a correct consumer must not depend on key order. In practice order still matters to people and to tooling. Reviewers read sorted config faster, snapshot tests break when a serializer changes order, and signing or hashing a payload needs one agreed byte sequence. The JSON Canonicalization Scheme (RFC 8785) solves the last case by sorting keys by UTF-16 code units, the same comparison this page uses, though it also normalises numbers and whitespace.

FAQ

Why does "Zebra" sort before "apple"?

The sort compares UTF-16 code units, and every uppercase A to Z (65 to 90) comes before every lowercase a to z (97 to 122). Digits (48 to 57) come before letters. That is why capitalized keys land at the top.

Does it sort nested object keys too?

Yes. Keys are sorted recursively at every level, including objects inside arrays, the same as jq -S . or Python json.dumps(data, sort_keys=True, indent=2).

How do I sort a JSON array?

This page sorts object keys, not array elements. Sort arrays in code, for example rows.sort((a, b) => a.price - b.price) in JavaScript or sort_by(.price) in jq.

What if the input is a JSON array, not an object?

That works. The array keeps its item order, and the keys of every object inside it are sorted. To reorder the items themselves, sort the array in code by a field.

Why do numeric keys come first, in number order?

JavaScript objects always place integer-like keys such as "9" and "10" first, in ascending numeric order, whatever order they were inserted in. Other keys follow in sorted order.

Is my data uploaded?

No. Sorting runs in your browser with Object.keys().sort(). Nothing is transmitted.