Overview
Introduction
JSON object key order is preserved by JSON.parse but rarely meaningful to the data itself, two objects with the same keys in different orders usually represent the same thing.
This tool normalizes key order so equivalent documents produce identical text, which matters for diffing, version control, and caching.
What Is JSON Object Key Sorter?
A recursive key sorter that rewrites a parsed JSON value with every object's own keys reordered alphabetically, at every nesting level, before re-serializing it.
It walks arrays too, not to reorder their items, but to make sure any objects nested inside array items also get their keys sorted.
How JSON Object Key Sorter Works
The tool parses the input, then recursively rebuilds every object by taking Object.keys(), sorting them with a string comparator (ascending or descending), and reinserting each key's value in that new order.
Arrays are walked item-by-item with the same recursive function so any object nested inside an array element is sorted too, but the array's own item order is left exactly as-is.
When To Use JSON Object Key Sorter
Use it before committing a JSON config file to version control so unrelated key reordering doesn't pollute future diffs.
It's also useful before comparing two JSON documents for semantic equality, since sorted keys make a simple text diff meaningful.
Often used alongside JSON Formatter & Beautifier, JSON Diff Checker and JSON Object Flattener.
Features
Advantages
- Makes two semantically-equal JSON documents byte-for-byte comparable.
- Recurses into every nesting level, not just the top-level object.
- Supports both ascending and descending order.
Limitations
- Sorting is a plain case-sensitive string comparison, not locale-aware collation, so mixed-case keys may sort in a way that looks odd to a human reader.
- It doesn't preserve any "logical" grouping of related keys; alphabetical order can separate keys that a human would want kept together.
Examples
Best Practices & Notes
Best Practices
- Sort keys before diffing two JSON documents that should be logically equivalent.
- Use ascending order for most config/version-control use cases; descending is mostly useful for specific display ordering.
- Combine with JSON Formatter's indent options if you also need to adjust spacing.
Developer Notes
The recursive sortKeys() function explicitly branches on Array.isArray() before the generic object case so array item order is never touched, only each object encountered (whether at the root or nested inside an array item) has its own Object.keys() re-sorted and rebuilt into a new object in that order.
JSON Object Key Sorter Use Cases
- Normalizing config files before committing them to version control
- Making two JSON documents' keys directly comparable in a text diff
- Producing a canonical, deterministic serialization of a JSON object
Common Mistakes
- Expecting array items themselves to be reordered; only object keys are sorted.
- Assuming the sort is locale-aware; it's a plain JavaScript string comparison, so case and Unicode ordering follow that rule.
Tips
- Use descending order when you specifically want a reverse-alphabetical key listing for display purposes.
- Pair with JSON Diff after sorting both sides to see only the meaningful value differences.