How to Sort JSON Keys Alphabetically Online (Nested Objects Included)
Sort JSON keys alphabetically online, at every level or only the top. Why a stable key order helps diffs and reviews, and what happens to arrays and numbers.
Updated November 8, 20269 min read
To sort JSON keys alphabetically online, open the JSON key sorter, drop in a .json file or paste the text, leave the order on Alphabetical and press Sort keys. Every object in the document, however deeply nested, comes back with its keys in A to Z order. Arrays keep their order unless you ask otherwise, and the values themselves are copied across character for character, so the only things that change are the order of the keys and the indentation. The rest of this guide explains why you would want that, when to sort only the top level, and the few cases that surprise people.
Why sort JSON keys alphabetically at all
A JSON object is, by definition, an unordered set of names and values. The standard leaves key order to whoever writes the file, so two programs exporting the same data can produce the keys in different orders: the order a database returned the columns, the order a developer added fields over the years, or the order a library happens to use internally. The data is identical. The text is not.
That difference matters the moment a person or a line-based tool reads the file. Sorting the keys gives every export one predictable shape, which helps in three everyday situations:
- Diffs in version control. When a config file is regenerated with its keys shuffled, a line-based diff marks half the file as changed. With sorted keys, the diff shows only the lines whose values really changed.
- Code reviews. A reviewer looking for
timeoutin a 300-line settings file finds it in the same place every time, and a duplicate or misspelled key sits right next to its correct twin, where it is easy to spot. - Stable output. Caches, checksums and snapshot tests that compare files byte for byte fail when nothing has changed but the order. A sorted, consistently indented file produces the same bytes for the same data.
If all you need is to see what changed between two versions, you do not have to sort first: a structural comparison ignores key order on its own, as explained in how to compare two JSON files. Sorting is for when the file itself has to be committed, reviewed or shipped in a stable form.
Sorting JSON keys, step by step
Add your JSON
Drop a .json, .jsonc, .ndjson or .jsonl file onto the JSON key sorter, browse for one, or paste the text into the box under the drop area.
Pick the order
Alphabetical is the default and what most diff tools and style guides expect. Choose Natural if your keys are numbered, Key length to put short keys first, or By value to order members by what they hold. Set Direction to A → Z or Z → A.
Decide how deep
Leave Depth on All levels to sort every nested object, or choose Top level only, 2 levels or 3 levels to keep the inner order as written.
Open Advanced if needed
Pin keys to the top, choose what happens to duplicate keys, sort arrays, or turn on Accept comments and trailing commas for JSONC files.
Sort and save
Press Sort keys, choose 2 spaces, 4 spaces, tab or minified output, then download the file or copy it. If you change a setting afterwards, the result is marked as using older settings so you can re-apply.
Four presets fill these settings in one click: Alphabetical, package.json, Diff-friendly and Minified. Diff-friendly uses natural order, ignores case, sorts arrays of plain values and ends the file with a newline, which is a sensible choice for a file that lives in a repository.
Alphabetical, natural or key-length order, at every level or only the top one, with values left exactly as written. Free: one document up to 25 MB, with every sorting option included. Pro: no size cap beyond what your device can handle, and up to 20 documents at once in one archive. It all runs on your device, not our servers.
Sort JSON keysRecursive or top level only?
Sorting is recursive by default: the outer object is sorted, then every object inside it, then every object inside those, all the way down. That is what you want for API responses, exported data and most generated files, because the point is a predictable shape everywhere.
Some files have a meaningful order inside their sections. A hand-written settings file might list a server's host before port before user because that is how people read it, while the sections themselves can go in any order. Set Depth to Top level only and the sections get sorted while everything inside them stays as written. The 2 levels and 3 levels options sit in between.
| Depth setting | What moves | Good for |
|---|---|---|
| All levels | Keys of every object, at any depth | API responses, exports, generated config |
| Top level only | Only the outermost keys (and the keys of each record in a top-level list) | Hand-ordered sections, package manifests |
| 2 levels / 3 levels | Keys down to that depth; deeper objects untouched | Large configs where the inner detail is ordered on purpose |
What happens to arrays
Arrays are left in their original order. Unlike object keys, array positions usually mean something: steps in a sequence, the ranking of search results, the layers of an image. Reordering them would change the data, not just its presentation. The keys of objects inside an array are still sorted by the normal rules; only the position of the elements is protected.
Two opt-in settings in Advanced change this when you do want it. Also sort arrays of plain values reorders arrays that contain only strings, numbers, booleans or null, such as a list of tags. Values are grouped by type first and then sorted within each group. Sort lists of objects by takes a property name such as id and orders a list of records by it; records missing that property keep their relative order at the end. Either setting leaves an array alone if it does not fit the rule completely, so you never get a half-sorted list.
What happens to numbers and other values
Many tools sort keys by parsing the JSON into memory and writing it back out. That round trip quietly changes values that were fine before. This sorter works on the text itself, so values come out exactly as they went in:
| In your file | After a parse and re-write | After this sorter |
|---|---|---|
| 12345678901234567890 | 12345678901234567000 | 12345678901234567890 |
| 1.0 | 1 | 1.0 |
| 1e5 | 100000 | 1e5 |
| "caf\u00e9" | "café" | "caf\u00e9" |
| The same key twice | Only the last one kept | Both kept and counted |
The first row is the one that causes real damage. Ordinary JSON numbers are stored as double-precision floating point, which holds integers exactly only up to 9,007,199,254,740,991. Long database IDs, order numbers and timestamps in nanoseconds are often larger than that, and a parse and re-write silently replaces their last digits. With the text left untouched, a sorted file differs from the original only in key order and whitespace.
Number-like keys are another trap. When a JavaScript program rebuilds an object, keys such as "10" or "2" are pulled to the front in numeric order, whatever order you asked for. Here they are sorted like any other key: as text in Alphabetical order, or as numbers in Natural order.
Alphabetical, natural or ignore case: which order is right
Alphabetical compares keys character by character by their Unicode code points. That has two effects people notice. Capital letters come before lowercase ones, so Zone sorts above apple; and accented letters come after the plain alphabet, so élan lands after zebra. Turn on Ignore case if Name and name should sit together instead of in two separate blocks.
Natural order reads digits inside a key as numbers, so item2 comes before item10. In plain alphabetical order item10 comes first, because the character 1 is lower than 2. Use natural order for numbered keys such as step1 to step12; keep alphabetical when the file has to match what another tool or a schema expects, since most of them sort that way.
Pinning sits on top of either order. Type keys into Pin these keys first, separated by commas, and they appear at the top in that order at every level, with the rest sorted beneath them. A trailing asterisk matches a prefix, so x-* keeps every extension field together. The package.json preset fills the list with the fields that file conventionally opens with, such as name, version and description.
When the sort goes wrong
- The file will not open. Invalid JSON is reported with the line, the column and a caret at the problem. A missing comma or an unquoted key is the usual cause; the JSON validator and JSON formatter help fix it.
- It has comments. Files like tsconfig.json or editor settings contain comments and trailing commas, which strict JSON forbids. Turn on Accept comments and trailing commas; each comment stays attached to the key below it and moves with it. Minified output cannot carry comments, so they are left out there.
- One document per line. NDJSON and JSON Lines files are detected automatically and every line is sorted on its own. The indent setting does not apply, because each record has to stay on one line.
- The file is too big. Free accepts one document up to 25 MB. If a file is larger, minifying it first often brings it under the cap, since indentation can make up a large share of a pretty-printed file.
- Keys you do not want at all. Sorting only moves keys. To drop fields such as internal IDs before sharing a file, use JSON Remove Keys first, then sort.
Frequently asked questions
How do I sort JSON keys alphabetically online?
Paste your JSON or drop the file into the JSON key sorter, keep the order on Alphabetical and press Sort keys. Every object at every level is sorted A to Z, arrays keep their order, and you can download or copy the result. Free covers one document up to 25 MB.
Does sorting JSON keys sort nested objects too?
Yes, by default. Depth is set to All levels, so objects inside objects and objects inside arrays are all sorted. Choose Top level only, 2 levels or 3 levels if you want deeper objects left as written.
Will sorting change the order of my arrays?
No, not unless you switch it on. Array order usually carries meaning, so only object keys move. In Advanced you can sort arrays of plain values, or sort a list of objects by a property such as id.
Can sorting JSON keys change my numbers?
Not with this tool. Values are copied from your file character for character, so 1.0, 1e5 and integers longer than 16 digits stay exactly as written. Tools that parse and re-write the JSON can round large integers and reformat numbers.
Why does item10 come before item2 when I sort?
Alphabetical order compares characters, and 1 sorts before 2, so item10 lands first. Switch the order to Natural, which reads the digits as numbers and puts item2 before item10.
Can I keep some keys at the top, like id or name?
Yes. Open Advanced and list them in Pin these keys first, separated by commas. They appear in that order above the sorted keys at every level. A trailing asterisk, as in x-*, pins every key with that prefix.