PrettyJSON Logo
PrettyJSON

Instantly clean, validate, and inspect web payloads

Sample Payloads
Buy me a coffee
Raw JSON Input

Waiting for JSON content

Start typing on the left or load one of the sample payloads to inspect it instantly.

RFC 8259 Compliant & 100% Client-Side

JSON Sorter Online — Sort JSON Keys Alphabetically

Instantly sort JSON keys alphabetically (A→Z or Z→A), recursively through every nested object. Values, data types, and array order stay intact — 100% in-browser, with zero data upload.

Private & Offline Ready

Every tool runs in your browser, so the JSON you paste never leaves your device.

Works Instantly, Even Offline

Formatting, minifying, repairing, and the tree view all run on your device, so there are no uploads to wait on.

Exact Error Line & Column

Syntax errors like unescaped quotes and trailing commas are flagged with their exact line and column.

Recursive Key Sorting Algorithms & Deterministic JSON Canonicalization

How alphabetical key ordering produces stable, diff-friendly JSON while preserving values, data types, and array element order.

1. Why Sort JSON Keys? Deterministic Output & Clean Diffs

JSON objects are unordered collections of name/value pairs per RFC 8259 §4, which means serializers in different languages and runtimes emit keys in different orders. This non-determinism creates noisy version-control diffs: two logically identical configuration files can show dozens of "changed" lines simply because key order shifted. Sorting object keys alphabetically produces a canonical, deterministic representation so that re-serialized API responses, database exports, and config files diff cleanly. PrettyJSON's sorter recursively orders keys at every nesting depth while leaving values and array element sequences untouched, guaranteeing the data remains semantically identical to the input.

Technical Highlights

  • Alphabetical key ordering produces canonical, deterministic JSON output.
  • Eliminates false-positive version-control diffs caused by shuffled key order.
  • Values, data types, and array element order are never modified.

2. Recursive Traversal: Sorting Nested Objects & Arrays

The sorter performs a depth-first recursive traversal of the parsed JSON tree. At each object node, keys are extracted, sorted using lexicographic string comparison (locale-aware), and rebuilt into a new object in the chosen direction — ascending (A→Z) or descending (Z→A). Array nodes are traversed element-by-element so that nested objects inside arrays are sorted too, but the array's own element order is intentionally preserved because array order is semantically meaningful in JSON. Primitive values (strings, numbers, booleans, null) are returned unchanged. This composition sorts arbitrarily deep structures in a single linear pass.

Recursive Alphabetical Key Sort (A → Z)
// Input (unsorted keys)
{
  "name": "Alice",
  "age": 30,
  "address": {
    "zip": "10001",
    "city": "NYC"
  }
}

// Output (keys sorted A → Z, recursively)
{
  "address": {
    "city": "NYC",
    "zip": "10001"
  },
  "age": 30,
  "name": "Alice"
}
Explanation: Top-level keys and the nested "address" object are both alphabetized, while values remain exactly as provided.

3. Practical Use Cases: Config Files, API Snapshots & Caching

Sorted JSON keys are valuable well beyond readability. Snapshot testing frameworks compare serialized JSON, and canonical key ordering prevents flaky test failures. Content-addressable caching and hashing (e.g., generating an ETag or cache key from a payload) require a stable byte representation — sorting keys before hashing ensures identical data always yields the same hash. Teams that commit JSON config files (package manifests, i18n translation catalogs, infrastructure descriptors) use key sorting to keep pull-request diffs minimal and review-friendly. PrettyJSON runs the entire sort in your browser, so even sensitive production payloads never leave your machine.

Technical Highlights

  • Stabilizes snapshot tests by canonicalizing serialized JSON output.
  • Enables consistent content hashing for cache keys and ETags.
  • Keeps committed config-file diffs small and easy to review.

Frequently Asked Questions

Common questions and expert answers regarding this tool.

What does the JSON Sorter do?

It reorders the keys of every object in your JSON alphabetically — either ascending (A→Z) or descending (Z→A) — recursively through all nested objects. Values, data types, and the order of array elements are preserved exactly.

Does sorting JSON keys change my data?

No. Sorting only reorders object keys, which are unordered by definition in RFC 8259 §4. The resulting JSON is semantically identical to the input — same values, same types, same array order.

Why would I sort JSON keys alphabetically?

Canonical key ordering produces clean, deterministic output. It minimizes version-control diffs on config files, stabilizes snapshot tests, and enables consistent content hashing for cache keys and ETags.