How to sort JSON object keys
By Flavio
Sort the keys of a JSON object in the browser. Arrays stay in order. You can sort nested objects, and you can ignore letter case.
Free toolThis tutorial uses the JSON Key Sorter. It runs in your browser, so open it in another tab and follow along.
Two JSON objects can hold the same fields and still look different, because key order is part of the text. The JSON key sorter rewrites an object so the keys are in a fixed order. The JSON stays in the browser. It is not stored in the URL.
Arrays keep their order. Only object keys move.
Sort the sample
- Open the JSON key sorter.
- Leave nested sorting and case-sensitive on. Leave minify off.
- Click Load sample.
- Copy writes the JSON. Download saves
sorted.json.
The sample is:
{
"b": 1,
"a": 2,
"B": 3,
"nested": {
"z": true,
"m": false
},
"tags": ["json", "keys"]
}
The sorted JSON is:
{
"B": 3,
"a": 2,
"b": 1,
"nested": {
"m": false,
"z": true
},
"tags": [
"json",
"keys"
]
}
The notes are:
Arrays keep their order.
JSON.parse keeps the last value when a key is repeated. This page cannot see the earlier one.
Case-sensitive order compares UTF-16 code units, the same comparison JavaScript uses for <. B comes before a, because the code unit for B is smaller. Inside nested, m comes before z. The tags array stays json, then keys.
Ignore letter case
Turn off Case-sensitive and run the same sample. The keys become a, then B, then b. The comparison uses toLowerCase(), which is the Unicode default case mapping, not a locale. The original spelling breaks a tie, so B still comes before b.
The extra note is: Keys are compared with toLowerCase(). The original spelling breaks a tie.
Turn off Sort nested objects and nested stays z then m, the order in the source. The note is: Only the top-level object is sorted. Nested objects keep their key order.
Minify writes one line, with no extra space. The default indent is two spaces.
An array root is rejected. The error is: The root has to be a JSON object. An array keeps its order, so there are no keys to sort.
Files up to 1 MB open from Open file. The page stops at 1,000,000 characters. The URL stores an option only when it is not the default: recursive=0, case=0, or min=1.
Introduction to JSON is the syntax. The Object keys() method explains the order Object.keys already uses, including numeric keys. Parsing JSON with Node.js is JSON.parse on the server.
JSON formatter only changes whitespace. JSON diff compares two values and ignores key order. JSON repair fixes JSON that does not parse, which you need before this page can sort it.
Try the JSON Key Sorter
Sort JSON object keys in the browser. Arrays keep their order. Case-sensitive sort uses UTF-16 code units. The JSON is not stored in the URL.
Related tools
- JSON Formatter & Validator: Validate, pretty-print, and minify JSON with clear error messages. Runs entirely in your browser.
- JSON Diff: Compare two JSON values in your browser and see every added, removed, changed, or moved field with its path. Key order is ignored.
- JSON Repair: Repair invalid JSON from an LLM in your browser. Fixes code fences, trailing commas, single quotes, comments, and cut-off brackets, and lists each fix.
Want me to talk about your product? You can sponsor this site.