To sort JSON keys alphabetically, reorder the members of every object by key name and leave arrays alone. Two JSON files can hold exactly the same data and still look different because their keys are in a different order. That makes diffs noisy, breaks caching and gives identical data different hashes. Sorting keys produces *canonical* JSON and fixes all three.
This guide explains why JSON key order has no meaning, how to sort object keys alphabetically at every level, what canonical JSON is and why hashing and signing depend on it, and the one thing sorting must never touch: array order.
Why JSON key order has no meaning
The JSON spec, RFC 8259, says an object is an *unordered* collection of name/value pairs. By the standard, {"a":1,"b":2} and {"b":2,"a":1} are the same object. A conforming parser treats them as equal, so no correct program should depend on key order.
That is why reordering keys is always safe and loses nothing. You change the presentation, not the data. In practice most parsers *keep* insertion order when you read an object back. That is convenient, but do not rely on it for correctness.
Why sort keys at all?
If order has no meaning, why sort? Because *people and tools* care about consistency even when the data model does not. A stable key order gives you concrete benefits:
- Clean diffs. When both versions of a file have sorted keys, a diff shows only real changes, not fields that moved. This is the biggest everyday benefit. See the JSON diff guide to compare two files.
- Stable hashes. Identical data must give an identical digest. Serialization order changes the bytes, so you must sort keys first or two equal objects hash differently.
- Reliable caching and deduplication. A serialized object only works as a cache key or dedup fingerprint if equal objects always serialize the same way.
- Readability. Alphabetical keys make large config files and API payloads easier to scan and search.
Sorting keys in code
Walk the structure, and rebuild every object with its keys in sorted order. This has to be recursive. A shallow sort only orders the top level and leaves nested objects unsorted. The walk goes into arrays to sort the objects inside them, but keeps the element order.
function sortKeys(value) {
if (Array.isArray(value)) {
// Recurse into elements, but KEEP array order.
return value.map(sortKeys);
}
if (value && typeof value === "object") {
return Object.keys(value)
.sort() // alphabetical by key
.reduce((acc, key) => {
acc[key] = sortKeys(value[key]);
return acc;
}, {});
}
return value; // primitives unchanged
}
const data = { b: 1, a: { d: 4, c: 3 }, list: [{ z: 1, y: 2 }] };
console.log(JSON.stringify(sortKeys(data)));
// {"a":{"c":3,"d":4},"b":1,"list":[{"y":2,"z":1}]}JSON.stringify also accepts an array of keys as its second argument, but that is an *allowlist* that drops any key you do not list. It does not sort. For canonical output, sort the structure first, as above, and then stringify.
What canonical JSON means
Sorting keys is the main step toward canonical JSON: one fixed serialization for a given piece of data, so equal data always produces identical bytes. This matters most for cryptography. To hash, sign or verify JSON, both sides must serialize it the same way, or the digests will not match even though the data does.
A full canonical form fixes more than key order. Formal schemes like JCS (JSON Canonicalization Scheme, RFC 8785) also specify number formatting, string escaping and whitespace. For everyday diffing and caching you rarely need the full standard, because sorted keys plus consistent whitespace are usually enough. The goal is the same: remove every meaningless difference so only real ones remain.
- Key order. Sorted, recursively. This matters most.
- Whitespace. Fixed, usually minified, so formatting cannot vary. See how to minify JSON.
- Number and string formatting. One representation per value, which full schemes like JCS define.
Sort JSON keys online without writing code
For a one-off file, a config you want in alphabetical order, or two payloads you are about to compare, you do not need a script. Paste the JSON, sort the keys and copy the result. Only the order changes, so it is safe to run on any valid JSON.
The JSON Sorter runs in your browser and never uploads your JSON, which matters when the payload holds tokens, keys or personal data. Sort both versions of a file the same way, and a JSON diff then shows only the real changes.
A typical workflow: sort keys to canonicalize, minify to fix whitespace, then hash or diff the stable result. Each step removes a source of meaningless variation, so the comparison reflects the data itself.