Sort JSON object keys

Sort JSON object keys recursively while preserving array order.

freeworks offlinenothing uploaded
ToolJSON Sort Keys
Input
Output
Put this on your own site

The frame below runs the same code as this page, in the reader's own browser. Nothing is sent to us, and nothing is sent to you.

Pick a dark background and the text and panels follow it, so the frame stays readable on a dark page.

Preview

How it works

Objects are traversed and their own keys sorted lexicographically in ascending or descending order, while arrays retain their original element order. The result is serialized with the chosen indentation.

  • Ascending lexical order is deterministic.
  • Arrays stay ordered because their sequence is data.

Worked example

Sort Keys A-Z
Sort object keys in ascending alphabetical order
Input
											{"zebra": 1, "apple": 2, "mango": 3}
										
Output
												{
  "apple": 2,
  "mango": 3,
  "zebra": 1
}
											

When to use this

Config normalization, snapshot tests, and signing preparation sort object members.

Edge cases

  • Case-sensitive JavaScript comparison is used instead of locale collation.
  • Array elements do not move even when their nested keys are sorted.
  • Sorting does not canonicalize numeric formatting outside the new serialization.

References