JSON Sorter

Sort JSON object keys alphabetically. Paste your JSON and configure sorting options:

Loading...
Loading...

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.json

Use 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

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.