ToolNimba

๐Ÿ”ค JSON Sort Keys Online

Shihab Mia By Shihab Mia ยท Updated 2026-08-03

Sorted JSON
 

Paste JSON above, then click Sort Keys to order every object key alphabetically.

This JSON Key Sorter takes any valid JSON and reorders every object key alphabetically, all the way down through nested objects. Array element order stays exactly as it was, because arrays are ordered by position, not by name. Choose ascending (A to Z) or descending (Z to A), pick your indentation, and copy the clean, sorted result. Everything runs in your browser, so your data never leaves the page.

What is the JSON Key Sorter?

JSON objects are, by the specification, an unordered collection of name and value pairs. That means {"b":1,"a":2} and {"a":2,"b":1} represent the same data. In practice, though, the textual order of keys matters a great deal for humans and tools: it determines how a file reads in a code review, whether two config files produce an identical diff, and whether a checksum or snapshot test matches. Sorting keys into a single canonical order removes that noise so the only differences you see are real value changes.

Sorting keys is a recursive job. The tool walks the parsed structure: when it meets an object it sorts that object's own keys, then descends into each value and sorts any objects it finds nested inside. When it meets an array it leaves the element order untouched (reordering array items would change the meaning of the data) but still recurses into each element so that objects sitting inside the array get their keys sorted too. Primitive values (strings, numbers, booleans and null) are passed through unchanged.

The comparison itself is a plain lexicographic (dictionary) sort on the key strings, using JavaScript's default string ordering by Unicode code point. That puts uppercase letters before lowercase ones (so "Zoo" sorts before "apple"), and it sorts digits before letters. If that surprises you, switch on the case-insensitive option, which folds keys to lowercase before comparing so "Apple" and "apple" land next to each other. The output is then re-serialized with your chosen indentation, giving you a tidy, deterministic, copy-ready document.

Sorted key order is also the basis of what is often called canonical JSON, a fixed serialization used whenever two systems need to agree byte for byte on what a document looks like. Content-addressed storage, JSON Web Signatures, hash-based cache keys and some blockchain formats all rely on a canonical form, and RFC 8785 (the JSON Canonicalization Scheme) specifically defines sorted keys as one of its rules. Command line tools reach the same result in different ways: jq produces it with the -S or --sort-keys flag, and Python's json.dumps accepts sort_keys=True. This tool gives you the same outcome without installing anything, which is handy when you just need a one-off comparison or a quick check before committing a file.

Because JSON key order carries no semantic meaning, sorting is always safe to apply to data you control. The one place to be careful is JSON that is itself a description of an ordered process, such as a list of migration steps encoded as object keys instead of an array, or a schema where a tool intentionally reads keys in file order (some linters and code generators do this, even though the JSON spec does not require it). For ordinary configuration files, API payloads and test fixtures, sorting keys is purely cosmetic and fully reversible in meaning, even though the exact original key order is not recoverable once you overwrite the file.

When to use it

  • Producing a canonical, diff-friendly version of a config or data file so version control shows only real changes.
  • Tidying an API response or fixture so related keys are easy to scan and locate by eye.
  • Normalizing two JSON files before comparing them, so key ordering does not create false differences.
  • Preparing JSON for snapshot tests or checksums where a stable, repeatable key order is required.
  • Building a canonical representation of a payload before hashing or signing it, similar to what jq -S or Python sort_keys=True produce.
  • Cleaning up a large, hand-edited JSON config file so related settings group together alphabetically instead of in the order they were added.

How to use the JSON Key Sorter

  1. Paste or type your JSON into the input box.
  2. Choose the sort order: A to Z (ascending) or Z to A (descending).
  3. Pick the indentation (2 spaces, 4 spaces or tab) and, if you like, tick case-insensitive.
  4. Click Sort Keys to see the recursively sorted JSON, then click Copy to grab the result.

Formula & method

For every object, keys are sorted with a lexicographic string comparison: ascending uses key A before key B when A is less than B by Unicode code point; descending reverses that. Arrays keep their index order. The sort recurses into nested objects and into objects inside arrays. Case-insensitive mode compares the lowercased keys.
Before"b": 1"a": {"d":4,"c":3}"z": [3,2,1]After (A to Z)"a": {"c":3,"d":4}"b": 1"z": [3,2,1]Object keys: sorted recursively at every depthArray elements: order preserved (index-based, not sorted)Result: a stable, canonical, diff-friendly JSON document

Worked examples

You have the object {"name":"Ada","age":36,"city":"London"} and want keys A to Z.

  1. Parse the JSON into an object with keys name, age and city.
  2. Collect the keys: ["name","age","city"].
  3. Sort them lexicographically ascending: age, city, name.
  4. Rebuild the object in that order and pretty-print it.

Result: { "age": 36, "city": "London", "name": "Ada" }

You have a nested object {"b":{"z":1,"a":2},"a":[3,2,1]} and want keys A to Z.

  1. Top-level keys b and a are sorted to a, b.
  2. The value of a is the array [3,2,1]: array order is preserved, so it stays [3,2,1].
  3. The value of b is an object {"z":1,"a":2}: its keys are sorted to a, z.
  4. The result reflects sorting at every object level while the array keeps its order.

Result: { "a": [ 3, 2, 1 ], "b": { "a": 2, "z": 1 } }

You have {"Zip":"10001","name":"Ada","Age":36} and want case-insensitive Z to A order.

  1. Lowercase each key for comparison only: zip, name, age.
  2. Sort those lowercased forms descending: zip, name, age.
  3. Map back to the original keys in that order: Zip, name, Age.
  4. Rebuild the object using the original (non-lowercased) key spellings.

Result: { "Zip": "10001", "name": "Ada", "Age": 36 }

What the sorter does to each JSON value type

Value typeAction taken
ObjectKeys sorted alphabetically; values recursed into
ArrayElement order preserved; each element recursed into
String / number / booleanReturned unchanged
nullReturned unchanged

How ordering options affect the keys Apple, banana and Cherry

OptionResulting key order
Ascending, case-sensitiveApple, Cherry, banana
Ascending, case-insensitiveApple, banana, Cherry
Descending, case-sensitivebanana, Cherry, Apple
Descending, case-insensitiveCherry, banana, Apple

Sorting JSON keys with this tool vs. common command line equivalents

MethodHow it sorts keys
This toolRecursive, ascending or descending, optional case-insensitive, no install
jq -S or jq --sort-keysRecursive ascending sort by Unicode code point, no descending option
Python json.dumps(data, sort_keys=True)Recursive ascending sort, requires writing a script
JSON Canonicalization Scheme (RFC 8785)Ascending sort by UTF-16 code unit, used for cryptographic canonical form

Common mistakes to avoid

  • Expecting array items to be sorted. This tool sorts object keys, not array values. Array order carries meaning (it is ordered by position), so reordering items would change your data. Elements inside an array still have their own keys sorted, but the items themselves stay where they are.
  • Forgetting that uppercase sorts before lowercase by default. The default comparison is by Unicode code point, so a key like "Zebra" comes before "apple". If you want "Apple" and "apple" grouped together, switch on the case-insensitive option.
  • Assuming sorting changes the meaning of the JSON. JSON objects are defined as unordered, so reordering keys produces an equivalent document. The values, types and structure are identical; only the textual order of keys changes.
  • Pasting JavaScript objects instead of strict JSON. Unquoted keys, single quotes, trailing commas and comments are valid JavaScript but not valid JSON, so the parser will reject them. Wrap keys and strings in double quotes and remove trailing commas first.
  • Sorting a file that a tool reads in insertion order. Most software ignores key order entirely, but a few code generators, some OpenAPI tools and certain hand-maintained migration files read keys top to bottom as an implicit sequence. Check whether your specific consumer depends on order before sorting a file it will read back in.
  • Expecting the original key order back later. Sorting is not reversible: once you save the sorted output over the original file, the previous key order is gone unless you kept a copy or it is recoverable from version control history.

Glossary

Key
The name part of a name and value pair inside a JSON object, always a string.
Lexicographic order
Dictionary-style ordering of strings, comparing characters one position at a time.
Recursive sort
Sorting that descends into nested objects and into objects held inside arrays, not just the top level.
Canonical form
A single, agreed representation of data (here, keys in a fixed order) so equivalent values look identical.
Pretty-print
Serializing JSON with line breaks and indentation so it is easy for a human to read.
Unicode code point
The numeric value assigned to a character; default string comparisons in JavaScript compare these values in order.
JSON Canonicalization Scheme (JCS)
RFC 8785, a standard that defines a single deterministic byte representation of JSON, including sorted keys, used for hashing and signatures.
Case-insensitive sort
A sort that compares lowercased versions of keys so letter case does not affect the resulting order.

Frequently asked questions

Does sorting keys change my data?

No. JSON objects are defined as an unordered set of name and value pairs, so reordering the keys produces a document that means exactly the same thing. All values, types and nesting stay identical; only the textual order of the keys changes.

Are arrays reordered too?

No, array element order is preserved. Arrays are ordered by position, so changing their order would change the meaning of your data. The tool does recurse into each array element, so any objects inside an array still get their own keys sorted.

Does it sort nested objects or only the top level?

It sorts every object at every depth. The sorter walks the whole structure, ordering the keys of nested objects and of objects that sit inside arrays, so the entire document ends up in a consistent order.

Why do uppercase keys sort before lowercase ones?

The default comparison uses Unicode code point order, where uppercase letters come before lowercase. That places "Name" before "age". Turn on the case-insensitive option if you want keys grouped without regard to letter case.

What happens if my JSON is invalid?

The tool shows a clear error message with the reason from the JSON parser and leaves the output empty. Common causes are single quotes, unquoted keys, trailing commas or comments, none of which are valid JSON.

Is my JSON sent anywhere?

No. All parsing and sorting happen in your browser with plain JavaScript. Nothing is uploaded, stored or sent over the network, so it is safe to use with private or sensitive data.

Is this the same as what jq --sort-keys does?

It produces the same kind of result, a recursive ascending sort of object keys with arrays left in place, but this tool also offers descending order and a case-insensitive option, and needs no command line or install.

Why would I want a canonical, sorted version of JSON?

A fixed key order means the same data always produces the same text. That matters for clean version control diffs, for comparing two JSON documents as plain strings, and for generating a stable hash or signature over the content, as used by schemes like RFC 8785 (JCS).

Can I sort keys in descending (Z to A) order?

Yes. Pick "Z to A" in the order option and the tool reverses the comparison at every object level, so the last key alphabetically appears first, both at the top level and inside every nested object.

Will sorting break an API that expects a specific key order?

For virtually all JSON consumers, no, because the JSON specification treats object key order as insignificant and correct parsers do not depend on it. The rare exception is a hand-written tool that intentionally reads keys as an ordered list; check your specific use case if you are unsure.