Resell services with the EthicalSMM API

JSON Key Sorter

Sort the keys of a JSON document alphabetically at every level, A to Z or Z to A, while keeping array order as it is.

Was this tool helpful?

Thanks for the feedback!

This tool sorts the keys of a JSON document alphabetically at every level of nesting, A to Z or Z to A, and leaves the order of array items as it is. Sorted keys make two versions of a file easier to compare by eye, give cleaner diffs in version control and make large configs easier to scan. It works in your browser, so the JSON is not sent anywhere.

The options

OptionChoices
OrderA to Z, or Z to A
Output format2 spaces, 4 spaces, Tabs, or Minified (one line)
Capital lettersIgnore case (a, B, c), or Uppercase first (B, a, c)

How the sorting works

The tool parses your JSON and visits every value. For an object, it sorts the keys and then sorts whatever is inside each key. For an array, it keeps the items in their original positions but still sorts the keys of any objects inside it. Plain values such as text, numbers and booleans are not touched.

With Ignore case, keys are compared in lower case, so address, Age and name come out in that order. If two keys differ only in case, the exact spelling breaks the tie. With Uppercase first, the comparison uses plain character order, where every capital letter A to Z comes before every small letter, so Age comes before address. The Keys sorted counter adds up keys at all levels.

Worked example

Input:

{"name":"Asha","Age":29,"address":{"pin":"411001","city":"Pune"},"skills":[{"level":3,"lang":"SQL"}]}

A to Z, Ignore case, 2 spaces (Keys sorted: 8):

{
  "address": {
    "city": "Pune",
    "pin": "411001"
  },
  "Age": 29,
  "name": "Asha",
  "skills": [
    {
      "lang": "SQL",
      "level": 3
    }
  ]
}

The same input with Uppercase first and Minified gives:

{"Age":29,"address":{"city":"Pune","pin":"411001"},"name":"Asha","skills":[{"lang":"SQL","level":3}]}

The skills array keeps its single object in place, but the keys inside that object are sorted.

Edge cases the tool cannot change

  • Number-like keys always come first. JavaScript keeps keys that look like whole numbers, such as "9" and "10", at the start of an object in ascending numeric order. So {"10":1,"9":2,"b":3,"a":4} sorted Z to A comes out as {"9":2,"10":1,"b":3,"a":4}. The letter keys are sorted, but the numeric ones stay at the front.
  • Duplicate keys collapse to one. If a key appears twice in the same object, the browser's JSON parser keeps the last value. {"a":1,"a":2} becomes {"a":2}.
  • Very large integers lose precision. Numbers are stored as 64-bit floating point, so 12345678901234567890 comes out as 12345678901234567000. Keep long IDs as strings.
  • Invalid input is rejected. A missing brace gives a message starting "The JSON is not valid:" with the position the parser stopped at.

When to use it

Different tools write keys in different orders. One editor saves a settings file with keys in the order you added them, a script rewrites it in another order, and a version control diff then shows the whole file as changed when only one value moved. If everyone sorts keys the same way before saving, the diff shows just the line that really changed. The same habit helps code review, because a reviewer can find a key by scanning alphabetically instead of searching.

Sort keys before committing a config or translation file so that later changes show up as small, clear diffs. Sort two API responses the same way before reading them side by side, or let the JSON diff checker list the differences for you, since it ignores key order anyway. If you only need to shrink a file, the JSON minifier does that without reordering anything.

Frequently asked questions

How do I sort JSON keys alphabetically?
Paste your JSON, choose A to Z and press Sort keys. The tool sorts keys at every level of nesting and lets you pick 2 spaces, 4 spaces, tabs or minified output.
Does sorting JSON keys change the data?
No. Object keys in JSON have no meaning by order, so the data is the same. The only exceptions are duplicate keys, where the last value wins, and integers too large for JavaScript to store exactly.
Are arrays sorted too?
No. Array items keep their original order because their position usually matters. Objects inside an array still have their own keys sorted.
Why do numeric keys stay at the top after sorting?
JavaScript always places whole-number keys such as "9" and "10" first, in ascending numeric order, before any other keys. No sorting option can move them.
What is the difference between Ignore case and Uppercase first?
Ignore case sorts address, Age, name as if all were lower case. Uppercase first uses character order, where capitals come before small letters, giving Age, address, name.
Can I sort keys and minify JSON at the same time?
Yes. Choose Minified in the Output format box and the sorted result comes out on a single line with no spaces.

Related guides

Related tools

People also used

Embed this tool on your site

Paste it into any blog or website. Free, with a credit link.