MergeJSON

How to Sort JSON Keys Alphabetically

Learn how to sort JSON keys A to Z or Z to A, recursively sort nested JSON objects, preserve or sort arrays, and create stable JSON for diffs and config files.

Published June 14, 2026

Object key order does not usually change what JSON means, but it can make a huge difference when people need to read, compare, review, or commit JSON files. To sort JSON keys means reordering object properties alphabetically while keeping the values intact.

This guide explains how to sort JSON keys online, when to sort nested JSON recursively, why arrays should usually be preserved, and how to create stable JSON for diffs, config files, API examples, and generated data.

The quickest way: sort JSON keys online

Paste JSON into the JSON Sort tool, choose A to Z or Z to A, and press Sort JSON Keys. The tool parses the input first, sorts object keys, and outputs valid JSON you can copy or download.

Use a browser-based JSON key sorter when working with private config, API payloads, localization files, package metadata, or customer exports. The sorting happens locally, so your data is not uploaded.

Use it when: you need stable JSON for Git diffs, code review, documentation, generated output, or side-by-side comparison.

What sorted JSON keys look like

Unsorted JSON:

{
  "version": 2,
  "name": "merge-json",
  "private": true
}

Sorted A to Z:

{
  "name": "merge-json",
  "private": true,
  "version": 2
}

The keys move. The values stay the same.

Sort nested JSON keys recursively

Many JSON files have objects inside objects. A top-level sort is useful, but a recursive JSON sort is usually better for real data:

{
  "config": {
    "zebra": true,
    "api": {
      "timeout": 3000,
      "baseUrl": "https://example.com"
    }
  }
}

After recursive key sorting:

{
  "config": {
    "api": {
      "baseUrl": "https://example.com",
      "timeout": 3000
    },
    "zebra": true
  }
}

Recursive sorting is helpful for configuration files, generated schemas, fixture data, lock-style metadata, and large API examples where consistent structure matters.

A to Z or Z to A?

Most people sort JSON keys A to Z because it matches normal alphabetical scanning and makes diffs predictable. Sorting Z to A is less common, but it can be useful when a team or generated format expects reverse alphabetical order.

The JSON Sort tool supports both:

  • A to Z - standard alphabetical JSON key sort.
  • Z to A - reverse alphabetical JSON key sort.

Preserve arrays or sort arrays too?

Arrays are different from object keys because array order often means something. For example, steps in a workflow, navigation items, leaderboard rows, and time-series values should not be reordered casually.

That is why the online sorter preserves arrays by default. It still sorts keys inside array objects when recursive sorting is enabled:

[
  { "name": "Beta", "id": 2 },
  { "name": "Alpha", "id": 1 }
]

becomes:

[
  { "id": 2, "name": "Beta" },
  { "id": 1, "name": "Alpha" }
]

If you intentionally want deterministic array order, choose Sort arrays too. That option is useful for primitive tag lists, simple sets, and generated data where array order is not meaningful.

Sort JSON keys in JavaScript

A simple recursive JSON key sorter looks like this:

function sortKeys(value) {
  if (Array.isArray(value)) {
    return value.map(sortKeys);
  }

  if (value && typeof value === "object") {
    return Object.fromEntries(
      Object.keys(value)
        .sort()
        .map((key) => [key, sortKeys(value[key])])
    );
  }

  return value;
}

Production tools add reverse sorting, array preserve-vs-sort controls, formatting, validation, error messages, and download support.

When should you sort JSON keys?

Sort JSON keys when humans need to review or compare the file. It helps with Git diffs, pull requests, API documentation, generated config, translation catalogs, JSON fixtures, and examples in docs.

Do not sort arrays unless you know array order is not important.

For one-off cleanup, use the online JSON Sort tool. After sorting, you can run the result through JSON Diff, JSON Formatter, or JSON Minifier depending on whether you need comparison, readability, or compact output.

Ready to merge your JSON?

Combine files or snippets in your browser — free and private.

Open the merge tool

Keep reading