JSON Sort Keys

Sort object keys alphabetically.

FreeNo accountOn your device

Runs in your browser. How to verify

Loading tool…

About this tool

Paste JSON and get it back with every object's keys sorted alphabetically, including nested objects and objects inside arrays. Array order is not changed. Sorted JSON makes diffs and merges much calmer.

How to use

1
Step 1
Paste valid JSON.
2
Step 2
Press Run.
3
Step 3
Copy the sorted JSON.

How it works

The tool parses the JSON, rebuilds each object with its keys in default JavaScript sort order (by UTF-16 code unit, so uppercase letters sort before lowercase), and prints with two-space indentation. Arrays keep their order, but objects inside them are sorted.

Worked example

Nested objects and an array

Input
{"b":1,"a":{"z":1,"c":[{"y":1,"x":2}]}}
Output
{ "a": { "c": [ { "x": 2, "y": 1 } ], "z": 1 }, "b": 1 }
Walkthrough
a now precedes b, c precedes z inside a, and x precedes y inside the array item.

Limitations

  • Sorting is by code unit, not by locale, so Z comes before a and 10 comes before 2.
  • Duplicate keys in the input collapse to the last value, as in any JSON parser.

Questions

How do I sort JSON keys alphabetically?
Paste the JSON and press Run. All objects are sorted by key at every depth.
Does it sort arrays too?
No. Array items keep their order, but objects inside arrays have their keys sorted.
Is the sort case sensitive?
Yes. Uppercase letters sort before lowercase letters.
Why sort keys before a diff?
Different key order shows up as noise in diffs. Sorting makes equal data look identical.

Features

  • Recursive key sort
  • Arrays keep order
  • Pretty-printed
  • Runs in your browser

Related resources

Guides and videos from around the web—opens on YouTube or the publisher’s site.

  • TypeScript in 100 Seconds

    Fireship

    Typed JSON payloads show up constantly next to format/validate tools.

    Watch on YouTube
  • How base64 encoding works

    Andrew Schmelyun

    Mental model for Base64 encode/decode tools.

    Watch on YouTube