JSON Sorter
Sort JSON object keys alphabetically. Paste your JSON and configure sorting options:
Why Sort JSON Keys?
Sorting JSON keys alphabetically provides several benefits for developers and teams working with JSON data:
- Easier comparison — Sorted keys make it easy to diff two JSON files and spot differences
- Consistent output — APIs and tools produce predictable JSON regardless of insertion order
- Better readability — Alphabetical organization helps you find keys quickly
- Version control friendly — Reduces noise in git diffs when JSON changes
- Testing — Makes snapshot testing more reliable
Sort Options Explained
Ascending vs Descending
Ascending (A→Z) sorts keys from a to z. Most common choice.
Descending (Z→A) sorts keys from z to a.
Recursive Sorting
When enabled, nested objects are also sorted. The tool traverses the entire JSON structure and sorts keys at every level.
// Before (recursive enabled)
{
"zebra": { "z": 1, "a": 2 },
"apple": { "b": 3, "a": 4 }
}
// After
{
"apple": { "a": 4, "b": 3 },
"zebra": { "a": 2, "z": 1 }
}Case Insensitive
By default, uppercase letters sort before lowercase (ASCII order). Enable case-insensitive sorting to ignore case:
// Case sensitive (default)
"Apple", "Zebra", "apple" → "Apple", "Zebra", "apple"
// Case insensitive
"Apple", "Zebra", "apple" → "Apple", "apple", "Zebra"Sort Array Values
When enabled, arrays containing primitive values (strings, numbers) are also sorted. Arrays of objects are not reordered — only their internal keys are sorted.
// Sort array values enabled
{ "tags": ["zebra", "apple", "mango"] }
→ { "tags": ["apple", "mango", "zebra"] }Example
Input JSON:
{
"name": "John",
"age": 30,
"address": {
"zip": "10001",
"city": "New York"
}
}Sorted output:
{
"address": {
"city": "New York",
"zip": "10001"
},
"age": 30,
"name": "John"
}Sorting in Code
JavaScript
function sortObject(obj) {
if (Array.isArray(obj)) {
return obj.map(sortObject);
}
if (typeof obj === 'object' && obj !== null) {
return Object.keys(obj)
.sort()
.reduce((sorted, key) => {
sorted[key] = sortObject(obj[key]);
return sorted;
}, {});
}
return obj;
}
const sorted = sortObject(myJson);Python
import json
def sort_json(obj):
if isinstance(obj, dict):
return {k: sort_json(v) for k, v in sorted(obj.items())}
if isinstance(obj, list):
return [sort_json(item) for item in obj]
return obj
sorted_data = sort_json(data)
print(json.dumps(sorted_data, indent=2))Command Line (jq)
# Sort keys recursively
jq -S '.' input.json > sorted.jsonUse Cases
Comparing API Responses
APIs may return keys in different orders. Sort both responses before comparing to focus on actual data differences.
Configuration Files
Keep configuration files sorted for easier maintenance and cleaner version history.
Test Fixtures
Sorted test data makes snapshot tests more stable and failures easier to diagnose.
Does Sorting Change the Data?
No! Sorting only changes the order of keys in objects. All values, data types, and structure remain identical. JSON objects are unordered by specification, so the sorted version is semantically equivalent.
However, note that array order IS significant. The "Sort array values" option is disabled by default because reordering array elements could change your data's meaning.
Related Tools
- JSON Diff — Compare two JSON files after sorting
- JSON Validator — Validate your JSON
- JSON Minify — Compress after sorting
- JSON Pretty Print — Format with sorting
Frequently Asked Questions
Does sorting change the JSON data?
No. Object key order is not significant in JSON. The sorted version contains exactly the same data, just with keys in alphabetical order.
How are nested objects handled?
Enable "Recursive" to sort keys in nested objects too. When disabled, only top-level keys are sorted.
Can I sort array values?
Yes, enable "Sort array values" to sort arrays of primitives (strings, numbers). Arrays of objects are not reordered.
What about numeric keys?
Numeric keys are sorted as strings by default. "10" comes before "2" alphabetically. If you need numeric sorting, you'll need custom code.
Common Mistakes & Pro Tips
- Object key order is not significant in JSON — RFC 8259 defines a JSON object as an unordered collection of name/value pairs, so {"a":1,"b":2} and {"b":2,"a":1} represent the same data. Sorting keys is therefore a safe normalization that does not change meaning.
- Sorted keys produce stable, minimal git diffs — When two tools or runs emit the same object with keys in different orders, a line-based diff shows noise. Sorting keys canonically before committing means only genuine value changes appear in the diff, which is the main practical reason to sort.
- Never sort the elements of an array — Array order IS significant in JSON, so a correct key-sorter recurses into arrays to sort the keys of objects inside them, but leaves the array element sequence untouched. Reordering array items would change the data.
- Sorting can break code that wrongly relies on key order — Although the spec says order is insignificant, some consumers iterate keys in insertion order (which JavaScript objects preserve for string keys). If downstream code depends on that order, re-sorting is a behavior change for them even though the JSON is technically equivalent.
- Choose a deterministic comparison for cross-system stability — Plain code-unit ordering (the default JavaScript string comparison) puts uppercase before lowercase and orders by Unicode value, which differs from locale-aware or case-insensitive sorting. Pick one rule and apply it everywhere so the canonical form is reproducible across machines.
Frequently Asked Questions
Does sorting keys change what my JSON means?
No, because JSON objects are unordered by definition in RFC 8259. Two objects with the same keys and values are equal regardless of key order, so any correct parser produces the same result before and after sorting.
Why would I want to sort JSON keys at all?
The biggest reason is reproducible output and clean version-control diffs: canonicalizing key order means files that hold identical data also look identical line by line. It also makes large objects easier to scan and helps when comparing two configs by eye.
Does this sort arrays too?
No, and it should not. Array element order carries meaning in JSON, so changing it would change your data. The tool recurses into nested objects (including objects nested inside arrays) to sort their keys, but preserves the order of array elements exactly.
How do I sort keys in deeply nested JSON?
Use the recursive option so the sorter walks every object at every depth, including objects inside arrays, and orders each one's keys. A shallow sort would only reorder the top-level keys and leave nested objects untouched, which is rarely what you want.
Will sorting ever break my application?
Only if your code incorrectly depends on a specific key order, which violates the JSON model. Standard parsing is order-independent, so well-behaved consumers are unaffected. If a consumer reads keys positionally, fix that consumer rather than avoiding sorting.
How are uppercase and lowercase keys ordered?
By default, comparison is by Unicode code unit, where all uppercase ASCII letters (A-Z) come before lowercase (a-z). So 'Zebra' sorts before 'apple'. If you need case-insensitive ordering, choose that option explicitly so the result is what you expect.