JSON Sort Keys

Recursively sort object keys in a JSON document alphabetically.

Open tool

Overview

Paste a JSON document and the tool recursively sorts every object's keys alphabetically while preserving array order and value types. The result is a normalised payload where structurally-identical documents always look the same - useful for diffs, deterministic builds, and golden-file tests.

It's for developers comparing two JSON outputs that should be equivalent but differ only in key order, and for build systems that need a canonical form for hashing or caching. Reach for it when stabilising snapshot tests, producing reproducible API fixtures, or hunting for the real difference between two configs.

How it works

JSON keys are unordered per the spec (RFC 8259) - but most serialisers emit them in insertion order, so two semantically-equal JSON documents can be diff-noisy if produced by different code paths. The sort walks the parsed JSON tree and re-emits every object's properties in code-point order (Unicode-aware, ASCII-only inputs sort the same as Array.prototype.sort() defaults).

Arrays are positional, so order is preserved. Nested objects inside arrays are sorted individually, so [{a,b},{b,a}] becomes [{a,b},{a,b}].

Examples

  • Top-level sort:
    {"name":"x","id":1,"active":true}
    
    {"active":true,"id":1,"name":"x"}
    
  • Nested sort:
    {"user":{"name":"x","id":1}}
    
    {"user":{"id":1,"name":"x"}}
    
  • Array preserves order:
    [3,1,2]  ->  [3,1,2]
    
  • Objects inside arrays each get sorted:
    [{"b":2,"a":1}]  ->  [{"a":1,"b":2}]
    

FAQ

Is sorted JSON still valid?

Yes - the JSON spec says object key order is unspecified, so any order is valid. Re-sorted JSON parses identically to the original.

Will this break my application?

It shouldn't - any conforming JSON parser treats key order as irrelevant. If your code depends on key order, that's a bug to fix.

Why is B before a?

ASCII uppercase letters sort before lowercase (A-Z is 65-90, a-z is 97-122). For case-insensitive sorting, normalise to one case before sorting.

Does it deduplicate keys?

JSON parsers typically keep only the last value when duplicate keys appear. The sorted output reflects that - one entry per key.

Try JSON Sort Keys

An unhandled error has occurred. Reload ×