{}JSONFiddleEditor
Tools/

JSON Sorter

Sort JSON object keys alphabetically or sort arrays by a field value.

Deterministic key ordering for JSON objects and arrays

JSON objects are technically unordered maps, but the order keys appear in matters when you're reading a config file, debugging a diff, or preparing a canonical form for hashing. This tool walks your JSON structure and reorders every object's keys using String.prototype.localeCompare with the { numeric: true } option, so "item2" sorts before "item10". For arrays of objects, you can specify a field name and the tool will sort array elements by that field's value using the same locale-aware comparator. Recursive mode applies sorting at every nesting level in a single pass.

How to sort

  1. 1Paste or type your JSON into the left editor panel.
  2. 2Choose sort direction: A-Z (ascending) or Z-A (descending).
  3. 3Enable "Recursive" to sort keys at every depth, not just the top level.
  4. 4To sort arrays, enter the field name to sort by in the "Array field" input (e.g., "name" or "age").
  5. 5Click Sort. The reordered output appears on the right.

Under the hood

  • `localeCompare` with numeric collationKeys are sorted using localeCompare({ numeric: true }), so "z2" comes before "z10" and case differences are handled with sensitivity: "base".
  • Recursive deep sortWhen enabled, the sort function walks the entire tree recursively, reordering keys in every nested object it encounters.
  • Array field sortingSpecify a field name and array elements are sorted by that field's stringified value. Numeric strings sort numerically thanks to the locale-aware comparator.
  • Direction toggleAscending and descending are not separate codepaths — the comparator result is simply negated for descending, keeping behavior consistent.
  • Non-destructiveValues, types, and array contents are preserved exactly. Only key order and (optionally) array element order change.

Real-world scenarios

  • Canonical JSON for hashingSort keys deterministically before SHA-256 hashing to ensure identical objects produce identical hashes regardless of original key order.
  • Cleaner diffs in version controlAlphabetically ordered keys in package.json, tsconfig.json, or terraform state files produce minimal, readable diffs.
  • API response comparisonSort two API responses by key before diffing to avoid false positives caused by non-deterministic serialization order.
  • Leaderboard / ranking dataSort an array of player objects by score descending to verify ranking logic without writing code.

Before and after

// Input (unsorted keys):
{
  "zebra": 1,
  "apple": [
    { "name": "Charlie", "age": 30 },
    { "name": "Alice", "age": 25 }
  ],
  "mango": { "yellow": true, "color": "orange" }
}

// Output (recursive sort, ascending, array field: "name"):
{
  "apple": [
    { "age": 25, "name": "Alice" },
    { "age": 30, "name": "Charlie" }
  ],
  "mango": { "color": "orange", "yellow": true },
  "zebra": 1
}

Questions

Does sorting change my data values?

No. Only the order of keys within objects and (optionally) the order of elements within arrays are changed. All values, types, and nesting remain identical.

How does numeric sorting work in key names?

The comparator uses localeCompare with the { numeric: true } option. This means "field2" sorts before "field10", unlike a naive ASCII sort where "field10" would come first.

What happens if the array field doesn't exist on some elements?

Elements where the specified field is undefined are left in their original relative order. The comparator returns 0 for those pairs, so they don't move.

Can I sort arrays of primitives?

Leave the "Array field" input empty. Primitive arrays are currently passed through without reordering. For sorting primitive arrays, use the Query Playground with a JSONata expression like $sort(data).

Related tools