About the JSON Sorter
Two JSON files with the same data but keys in a different order produce noisy diffs, and translation or configuration files grow messy as people add keys in random places. This tool will sort JSON keys alphabetically in every object, recursively through the whole document, in ascending or descending order, and can sort arrays of simple values too. Output can be pretty-printed or minified. Sorted files make code reviews and comparisons much easier.
The document is parsed with the browser's JSON parser, which reports the line and column of any syntax error, then rebuilt with each object's keys in the chosen order. Arrays keep their order by default, because in JSON the order of array items usually matters; turn on array sorting to sort arrays of strings, numbers and booleans, while arrays of objects keep their order. The output is written with its own serializer, so even numeric keys such as "10" and "9" follow the chosen order instead of JavaScript's built-in key ordering.
Example: {"b":1,"a":{"d":2,"c":3}} becomes {"a":{"c":3,"d":2},"b":1}, shown with two-space indentation by default. In descending order, "b" comes first.
Sorting changes only the order, never the values. To format or validate JSON without sorting use the JSON formatter, to minify it the JSON minifier, and to generate TypeScript types from it the JSON to TypeScript converter.
How to use the JSON Sorter
- Paste your JSONAn object, an array or a whole file.
- Choose the orderA to Z or Z to A, with case-insensitive or natural numbers.
- Choose arrays and formatSort arrays of plain values too; pretty or minified output.
- Copy the resultCopy or download the sorted JSON.
JSON Sorter features
- Recursive key sorting
- Ascending or descending
- Case-insensitive and natural order
- Optional sorting of value arrays
- Pretty or minified output
- Correct order for numeric keys
- Errors with line and column
Common uses
- Comparing JSON files in diffs
- Tidying config and package files
- Translation and locale files
- Normalizing API responses for tests
- Code review of data files
Sorting JSON in practice: translation files, fewer merge conflicts, files with a conventional order, stable hashes and cache keys
Sorting keys is a small change that solves several practical problems: translation files that are impossible to compare, merge conflicts in shared configuration, and identical data producing different hashes because keys appeared in a different order. This guide works through tidying English and Urdu translation files and covers merge conflicts, automation, files where alphabetical order is the wrong choice, canonical JSON for hashing and signing, cache keys and sorting arrays of objects.
A worked example: translation files
An app keeps its interface text in en.json and ur.json. Over a year, developers have added keys wherever they happened to be editing, so cart.empty sits near the top of one file and the bottom of the other. Sorting both files puts every key in the same position, so the two can be reviewed side by side, and a missing Urdu translation shows up as a visible gap instead of hiding in a long, unordered list.
Fewer merge conflicts
When two developers each add a key at the end of the same file, their changes collide and Git reports a conflict. In a sorted file, new keys land in different places according to their names, so simultaneous additions rarely touch the same lines. Keeping shared JSON files sorted is a cheap way to reduce interruptions in busy teams.
Keeping files sorted automatically
Sorting once helps only until the next unsorted addition. Adding a formatting step to a pre-commit hook or continuous integration check keeps files sorted permanently, and fails the check if someone commits an unsorted file. Developers then never have to think about order again.
Files with a conventional order
Some files have an order people expect that is not alphabetical. In a package.json file, readers expect name and version first, followed by scripts and dependencies, and sorting alphabetically scatters these familiar sections. Dedicated tools sort such files in their conventional order. Use plain alphabetical sorting for data files, translations and settings without an established layout.
Canonical JSON for hashes and signatures
The same object written with keys in a different order, or with different spacing, produces a different hash. Systems that hash or sign JSON therefore define a canonical form, typically with keys sorted and no extra whitespace, so {"b":1,"a":2} and {"a":2,"b":1} produce identical output before hashing. Standards such as the JSON Canonicalization Scheme define these rules precisely.
Stable cache keys
Applications sometimes build cache keys from request parameters stored as JSON. If one part of the code writes the parameters in a different order, the cache misses and the work is repeated. Sorting keys before creating the cache key, or using a stable serialiser, means identical requests always map to the same cache entry.
Comparing without a diff tool
When only a plain text comparison is available, sorting both documents first removes differences caused purely by key order, so the comparison shows real changes. A semantic diff that understands JSON is better still, but sorting is a quick improvement for any text-based comparison or code review.
Sorting arrays of objects
Sorting keys does not reorder arrays of objects, because their order often carries meaning. To sort a list of products by ID or name, sort the array by that field in code, or with a command-line tool, as a separate step. Doing this before comparing two exports aligns matching records.
Case and accents
Strict character-code ordering puts all capitals before lower-case letters, so Zebra comes before apple. Case-insensitive ordering is more natural for people reading the file. Keys containing accented letters or Urdu characters sort by their character codes, which may not match dictionary order, so keep translation keys in plain English even when the values are Urdu.
Common mistakes
- Sorting once without automating it.
- Alphabetising files that have a conventional order.
- Hashing JSON without a canonical form.
- Expecting key sorting to reorder arrays of objects.
Related tools
To compare documents by meaning, use JSON Diff. To hash a canonical document, try SHA-256 Generator, and to sort plain lines of text, Text Sorter.
Naming keys so sorting groups them
Alphabetical sorting groups related keys automatically when names share a prefix, such as checkout.title, checkout.button and checkout.error. Choosing prefixed names from the start turns a sorted translation or settings file into a neatly organised document, where every screen's text sits together without any manual arrangement.
Generated files
Files exported by translation platforms, schema tools and admin panels often arrive with keys in a different order each time. Sorting them before committing means a diff shows only real changes, such as one new translation, instead of hundreds of moved lines that hide the change reviewers need to see.
Sorting settings exported from tools
Editors, linters and admin panels often export settings as JSON in whatever order they store them internally. Sorting an exported settings file before saving it in a repository, and again after each change, makes it easy to see which single option someone switched when the tool's behaviour suddenly changes for the whole team.
Privacy
Sorting runs in your browser. Your JSON is not uploaded or stored.
Frequently asked questions
How do I sort JSON keys alphabetically?
Paste the JSON and the keys of every object, at every level, are sorted A to Z. Choose descending for Z to A, then copy or download the result.
Does sorting change my data?
No. Only the order of keys changes; every value, string, number and nested structure stays the same. JSON objects are unordered by definition, so the meaning is unchanged.
Are arrays sorted too?
Not by default, because array order usually matters. Turn on sort arrays to sort arrays of strings, numbers and booleans. Arrays containing objects keep their order.
What is natural sort order?
Sorting that treats numbers inside keys as numbers, so key2 comes before key10. Plain alphabetical order puts key10 first, because 1 comes before 2. Use it for keys that contain version numbers, sizes or numbered items.
Why are numeric keys out of order in other tools?
JavaScript always lists integer-like keys first in numeric order. This tool writes the JSON itself, so keys such as "10" and "9" follow the order you chose.
Can I sort a large JSON file?
Yes, files of several megabytes sort in a moment in your browser. Nothing is uploaded, so the size is limited only by your device's memory.
How do I sort JSON keys with jq?
Use jq --sort-keys . file.json, or jq -S. In Python, json.dumps(data, sort_keys=True, indent=2) does the same. Both produce the same alphabetical order as the A to Z option here.
What if my JSON is invalid?
The tool shows the line and column where parsing failed and a hint for common mistakes such as trailing commas or single quotes. Fix the error and the sorted output appears.
Does key order matter in JSON?
Not to the data: JSON objects are unordered collections of names and values. Order matters to people reading files, to diffs and version control, and to tests that compare serialized output.
Can I sort keys in VS Code?
There is no built-in command for nested JSON keys, although extensions exist. Pasting the file here, sorting and pasting back is a quick alternative that needs no installation.