Sorting JSON Keys: Alphabetical Order, Canonical JSON and Signatures

Last updated: · 7 min read

The order of keys in a JSON object carries no meaning, yet it matters to people reading the file and to tools that work line by line. When two programs write the same data with different key orders, version control fills up with spurious changes, comparing two files by eye becomes hard, and identical content produces different hashes. The JSON Key Sorter arranges the keys of every object in a document by one rule at every level, leaving array order and values untouched. This guide explains which rule is used, why the result can differ from what you would call alphabetical, how sorting relates to canonical JSON (RFC 8785), and what to watch out for when signatures or hashes are involved. The output shown is the tool's real result, and everything runs in your browser.

Formula

Rules:
1. The keys of every object in the document are sorted, including nested objects and objects inside arrays.
2. Keys are compared by their UTF-16 code unit values (the rule used in RFC 8785). For ASCII characters the order is:
   digits (0–9) < uppercase letters (A–Z) < underscore (_) < lowercase letters (a–z) < non-ASCII letters (é, ö, ñ …)
3. Keys made of digits are compared as text too: "10" comes before "9".
4. The order of array elements does not change; array order is part of the data.
5. Values do not change. Numbers are kept exactly as written (1.50, 1E3, 12345678901234567890); strings keep the same value.
6. The order can be A → Z or Z → A; the output is indented with 2 spaces, 4 spaces or a tab, or minified to one line.
7. If an object contains a duplicate key, only the last occurrence is kept.

Input (order: A → Z, indentation: 2 spaces):
{"name":"Ada","id":1,"tags":["b","a"],"address":{"zip":"10115","city":"Berlin"}}

Output:
{
  "address": {
    "city": "Berlin",
    "zip": "10115"
  },
  "id": 1,
  "name": "Ada",
  "tags": [
    "b",
    "a"
  ]
}

This is a sorting rule, not a formula. The comparison does not depend on the locale: the tool ignores the alphabet rules of whatever language you use and compares the code values of the characters. The same input therefore gives the same output on every machine and under every language setting, which is essential if sorting is to reduce diffs and stabilise hashes. The price is that the order does not look like a dictionary: keys starting with an uppercase letter come before all lowercase ones, and accented letters come after z.

What the inputs mean

JSON
The document whose keys are to be sorted. The root may be an object or an array; for an array, the keys of every object inside it are sorted while the elements stay in place.
Order
A → Z (default) arranges keys by ascending code value, Z → A by descending. The choice applies the same way at every level.
Indentation
2 spaces (default), 4 spaces, tab, or minified (single line). Indentation only changes the appearance; if you are going to compare hashes or signatures, use the same option on both sides.

Worked example: Sorting the keys of a record

  1. The input has its keys in the order name, id, tags, address. Sorted A → Z, the root object's keys become address, id, name, tags.
  2. address is a nested object; its keys are sorted as well, so zip and city swap places and city comes first.
  3. tags is an array; its elements stay in the order "b", "a". The tool does not sort arrays, because in an array the order is part of the data.
  4. With Z → A and minified output, the same input becomes a single line: {"tags":["b","a"],"name":"Ada","id":1,"address":{"zip":"10115","city":"Berlin"}}
  5. Code unit order: minified and sorted A → Z, {"b":1,"a":2,"B":3,"_x":4,"10":5,"9":6,"é":7,"z":8} gives {"10":5,"9":6,"B":3,"_x":4,"a":2,"b":1,"z":8,"é":7} "10" comes before "9" because keys are compared as text, not as numbers; uppercase B comes before the underscore and before every lowercase letter; and é comes after z.
  6. Number spelling is preserved: {"price":1.50,"id":12345678901234567890,"e":1E3} becomes {"e":1E3,"id":12345678901234567890,"price":1.50} 1.50 keeps its trailing zero, 1E3 its exponent notation, and the twenty-digit id every digit.
  7. A duplicate key and escapes: {"b":"caf\u00e9 \/ x","a":1,"a":2} becomes {"a":2,"b":"café / x"} Of the two a keys the last one survives, and the escapes \u00e9 and \/ are written in their plain form, which has the same value.

Result: In the first example the root keys end up in the order address, id, name, tags and the keys inside address in the order city, zip; the tags array stays ["b", "a"]. Output for Z → A, minified: {"tags":["b","a"],"name":"Ada","id":1,"address":{"zip":"10115","city":"Berlin"}}

🔤 JSON Key Sorter

Free, no sign-up; your data stays in your browser.

Open the calculator →

How to read the result

The most common reason to sort keys is stable diffs: if configuration and translation files are brought into the same order on every save, version control shows only the lines that really changed. The second is comparison: once two responses are in the same order, even a plain text diff gives a meaningful result. The third is hashing and signing, and that is where care is needed. A hash is computed over bytes; the same data with a different key order, whitespace or number spelling yields a different hash. RFC 8785 (JCS) defines a canonical form for this purpose: keys are sorted by UTF-16 code units, no whitespace is written, and numbers and strings are serialised by one fixed rule. The minified A → Z output of this tool follows that rule as far as key order goes, but it does not rewrite numbers: 1.50 and 1E3 stay as they are, whereas JCS requires 1.5 and 1000. Use a JCS library to create or verify signatures, and use this tool to tidy documents and compare them by eye.

Common mistakes

  • Expecting arrays to be sorted too. The tool sorts object keys only; ["b","a"] stays as it is, because changing the order of an array changes the data.
  • Expecting dictionary order. The comparison goes by code value: Zeta comes before alpha, and keys starting with letters such as é, ö or ñ are sorted after z.
  • Expecting numeric keys in numeric order. "10" and "9" are compared as text and "10" comes first; if you need numeric order, pad the keys with leading zeros ("09", "10").
  • Treating sorted output as canonical JSON and signing it. The tool does not change how numbers are written, so a signature produced according to RFC 8785 may fail to verify.
  • Producing the two documents you want to compare with different indentation options. Indentation changes the bytes; use the same option on both sides before comparing hashes or text.
  • Assuming duplicate keys are preserved. When a key occurs twice in the same object, only its last value is written to the output; the earlier ones are lost.
  • Sorting a file in which people rely on a particular key order. JSON says order is insignificant, but some tools and readers are used to a certain layout; do not reorder shared files without agreeing on it with your team.

Frequently asked questions

Why should I sort JSON keys?

There are three common reasons: version control then shows only real changes, two documents become easy to compare, and the same data produces the same text every time. The last point helps with cache keys and hashes; for work that demands exactness, such as signatures, a full canonical form is needed.

Does key order matter in JSON?

Not as far as the data is concerned. RFC 8259 defines an object as an unordered collection of name/value pairs and notes that parsing libraries differ in whether they make member order visible to the calling software. Software that depends on the order can therefore break when it meets a different library.

Why do uppercase letters come before lowercase ones?

The comparison uses UTF-16 code unit values, and uppercase letters have smaller codes (A = 65) than lowercase ones (a = 97). The rule is independent of the locale, so the same input gives the same order on every system. RFC 8785 uses the same comparison for property names.

Does the tool sort arrays as well?

No. In JSON an array is an ordered sequence of values, and changing the order changes the document. The keys of objects inside an array are sorted, but the elements stay where they are.

Is the output canonical JSON (RFC 8785)?

Not quite. The minified A → Z output matches JCS in key order and whitespace, but JCS also requires numbers to be written in one fixed form (1.5 rather than 1.50, 1000 rather than 1E3) and does not accept duplicate keys. The tool leaves numbers as written. If the numbers in your document are already in their shortest form the result will often be the same, but do not rely on that for signatures.

Are very large numbers or decimals altered?

No. The tool does not parse numbers and write them again; it copies their spelling from the text. Values such as 12345678901234567890, which do not fit in a 64-bit floating-point number, and trailing zeros as in 1.50 stay exactly as they are.

Is my JSON sent to a server?

No. Sorting runs in your browser. It is still good practice to remove access tokens and personal data from documents before pasting them anywhere.

The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.

Sources

Choose your cookie preference to continue

Records the site needs to work (theme, language, favourites) stay in your browser.

This site is free and funded by ads.

With your consent, Google Analytics cookies measure visits and Google AdSense cookies are used to show ads; the data is processed on Google servers abroad.

If you reject, the site works just the same.

Cookie Policy · KVKK notice