How to sort JSON keys alphabetically
JSON objects have no required key order, but files that people read and compare do. To sort json keys alphabetically, the tool reads your JSON, orders the keys of every object, and writes it back with the layout you choose. Values are not touched.
The built-in sample is a package.json style object on one line. With the default options it becomes:
{
"dependencies": {
"axios": "^1.7.0",
"react": "^18.3.1",
"zod": "^3.23.0"
},
"devDependencies": {
"typescript": "^5.5.0",
"vitest": "^2.0.0"
},
"engines": {
"node": ">=20"
},
"id": 9007199254740993123,
"name": "demo-app",
"scripts": {
"build": "vite build",
"dev": "vite",
"lint": "eslint .",
"test": "vitest"
},
"version": "1.2.0"
}The status line reports 5 objects and 15 keys moved. The What moved panel gives the detail for each object, for example axios 3 to 1 and zod 1 to 3 inside the dependencies. That is what makes a pull request easy to review: you can see which changes are only reordering.
Code point, case-insensitive and natural order
Code point order compares the character codes, so every capital letter comes before every lowercase letter. It matches Python's sort_keys and a sorted JSON.stringify, which makes it the right choice when another tool must produce the same file.
{"A":4,"B":2,"a":3,"b":1}Case-insensitive order ignores capitals, so A and a sit together:
{"A":4,"a":3,"B":2,"b":1}Natural order reads digit runs as numbers, so item2 comes before item10. It also ignores capitals. Use it for translation keys and anything numbered:
{"item1":3,"item2":2,"Item3":4,"item10":1}When two keys are equal in the chosen order, the one that came first in your file stays first.
Pin keys first and sort one level or all levels
Some files read better with a fixed head. Type name,version in Pin keys first and those keys stay at the start of the top-level object in that order, then everything else is sorted:
{"name":"app","version":"1.0.0","license":"MIT","scripts":{}}Pinned names apply to the top-level object, and to each object when the top level is an array. Nested objects are sorted normally. Set Levels to Top level only if inner objects should keep the order they have now.
Sort JSON object by key inside arrays
Arrays keep their order by default, because the order of items often carries meaning: steps, a ranking, coordinates. Two options change that on purpose. Sort plain values orders arrays that hold only numbers, text or true, false and null. Sort objects by a key orders an array of records by the value of one key, such as id.
[{"id":1,"name":"Ann"},{"id":2,"name":"Bo"},{"id":3,"name":"Cy"}]That is the result for three records entered in the order 3, 1, 2 with the key set to id. Numbers sort by value, text uses the chosen order, and records without the key go last. The sort is stable, so records with the same id keep their order.
Long numbers, strings and repeated keys
Many sorters parse the JSON into JavaScript values and print it again, which rounds numbers beyond 15 or 16 digits. This tool moves the original text of every value instead. In the sample, 9007199254740993123 comes out as 9007199254740993123, and 1.0, 1E+5 and 0.10 keep their form. Strings keep their escapes too, so \u00e9 stays \u00e9.
A repeated key is not merged or dropped. Both copies stay, in their original order, and the Repeated keys list names the object. Most readers use the last copy, so you may want to remove one by hand.
Pretty output ends with a line break, like a file saved by most editors. Minified output does not.
Sort JSON keys in Python, JavaScript and on the command line
- Python: json.dumps(data, sort_keys=True, indent=2) sorts keys in code point order.
- JavaScript: JSON.stringify has no sort option. You rebuild each object with its keys in order, and keys that look like whole numbers, such as "10", are always listed first by the language.
- Command line: jq -S . file.json sorts keys.
- Editors: VS Code's built-in Sort Lines Ascending sorts lines, which breaks nested objects. Use this page, or a JSON-aware extension.
This page is handy when you do not want to write code, when the file holds secrets you will not paste into a chat, or when you want to see which keys moved.
Limits and privacy
The input must be valid JSON. Comments and trailing commas are accepted only when Allow comments and trailing commas is on, and they are removed from the output, because JSON has no place for them. JSON5 and other dialects are not supported, and the tool does not check a schema.
Sorting runs in your browser and nothing is uploaded. The Privacy Policy has the details.
Related tools
Need to clean text inside the values? Use JSON Escape and JSON Unescape. To change the format, try JSON to YAML or JSON to TOML. To order plain lines, use Sort Lines, and to compare two lists, use Compare Two Lists.