Xerobit

JSON Sort Keys: Alphabetize Your JSON Objects

Use this json sort keys tool to alphabetize every key in a JSON object, including deeply nested structures. Paste your input and the sorted result appears instantly. It runs entirely in your browser so nothing is uploaded.

Valid JSON with object keys to sort.

Sorted JSON

{
  "apple": 2,
  "mango": {
    "available": true,
    "color": "orange"
  },
  "zebra": 1
}

How to use the json sort keys

  1. Paste or type your JSON into the JSON input text area.
  2. Set the Indent spaces field to control formatting (use 0 for a single line).
  3. The sorted JSON appears automatically as you type or paste.
  4. Copy the result from the Sorted JSON output box.

How sorting works

The tool first checks your text with JSON.parse. If parsing fails, it shows the exact error message from the parser so you can fix typos or missing quotes.

Once the text is valid, a recursive function walks the structure. When it encounters an object, it sorts the keys with the default Array.prototype.sort() method and rewrites the pairs in that order, including keys that look like numbers ("10" sorts before "2", as a string sort should). Numbers are copied exactly as you typed them, so large integers such as 12345678901234567890 and values like 1.0 are not rounded or reformatted. Arrays are traversed element by element so that any objects inside them are also sorted. Primitives like strings, numbers, booleans and null are returned unchanged.

The default sort compares strings by their UTF-16 code unit values. This means uppercase ASCII letters come before lowercase ones ("Z" before "a") because their code points are lower. For most API payloads and configuration files this deterministic ordering is exactly what you need when you json sort keys for version control or testing.

Why sort JSON keys

Deterministic key ordering makes JSON comparisons reliable. When two systems generate the same logical data but emit keys in different orders, a simple string diff will flag every line as changed. Sorting keys first eliminates these false positives.

Common use cases include:

  • Version control diffs: committing configuration files with sorted keys keeps Git history clean and readable.
  • Snapshot testing: test frameworks that serialize objects to JSON produce stable snapshots when keys are ordered.
  • Canonical forms: cryptographic signing or hashing of JSON payloads requires a single canonical representation. Sorting keys is the first step toward that goal.
  • Data inspection: finding a specific field in a large payload is easier when keys are alphabetical.

If you need to compare two JSON documents after sorting them, a dedicated JSON diff tool can highlight the remaining differences.

Handling edge cases

Not every JSON value is an object. The tool handles all valid JSON types gracefully:

Input typeBehaviour
ObjectKeys sorted recursively at every depth.
ArrayElement order preserved; objects inside are sorted.
String, number, boolean, nullReturned as-is after re-serialization.
Empty object {}Returns {}.
Empty array []Returns [].

Unicode keys are fully supported. Characters outside the basic Latin range are compared by their code point, so "éclair" sorts after "z" because e with an accent has a higher code point than plain z. If you need locale-aware sorting, you would typically handle that in application code rather than a general-purpose formatter.

Very deep nesting is handled up to the JavaScript engine's call stack limit, which is typically several thousand frames. For typical API responses and config files this is never an issue.

Formatting options

The indent field controls how JSON.stringify formats the output. A value of 2 produces the familiar two-space indentation used by most linters and editors. Setting it to 0 removes all whitespace, producing a compact single-line string suitable for network transmission or storage where size matters.

After sorting, you might want to convert the JSON into another format. Tools like JSON to XML, JSON to HTML table or JSON to markdown table accept standard JSON and transform it accordingly. If you are generating typed models, you can pass the sorted output to converters such as JSON to Go struct, JSON to Java class, JSON to C# class, JSON to Python class or JSON to SQL.

Reference: sort order examples

The following table shows how various key sets are ordered by the default Unicode comparison used by this json sort keys tool:

Original keysSorted order
"b", "a", "c""a", "b", "c"
"Z", "a", "A""A", "Z", "a"
"10", "2", "1""1", "10", "2"
"_id", "id", "Id""Id", "_id", "id"

Note that numeric strings sort lexicographically, not numerically. "10" comes before "2" because the character 1 has a lower code point than 2. This matches the behaviour of Object.keys().sort() in JavaScript and ensures consistency across environments.

Worked examples

ExampleInputResult
Simple flat objectJSON input: {"zebra": 1, "apple": 2, "mango": 3}; Indent spaces: 2{ / "apple": 2, / "mango": 3, / "zebra": 1 / }
Nested objectsJSON input: {"z": 1, "a": {"c": 3, "b": 2}, "m": [1, 2]}; Indent spaces: 2{ / "a": { / "b": 2, / "c": 3 / }, / "m": [ / 1, / 2 / ], / "z": 1 / }
Compact outputJSON input: {"b": 2, "a": 1}; Indent spaces: 0{"a":1,"b":2}
Unicode keysJSON input: {"éclair": 1, "apple": 2, "über": 3}; Indent spaces: 2{ / "apple": 2, / "éclair": 1, / "über": 3 / }

FAQ

Does this change the order of items in arrays?

No. Array elements keep their original positions. Only the keys inside objects are reordered alphabetically.

What happens if my JSON is invalid?

The tool displays the exact parse error returned by JSON.parse, including the position where parsing failed, so you can correct the syntax.

Is the sort case-sensitive?

Yes. It uses standard Unicode code-point comparison, meaning uppercase letters sort before lowercase ones.

Can I use this for very large JSON files?

It works well for typical API responses and configuration files. Extremely large files may cause the browser tab to slow down due to memory limits.

Does sorting keys change the meaning of my data?

No. In JSON, object key order is semantically insignificant. Sorting only changes the textual representation, not the data itself.

Is my data sent to a server?

No. Parsing, sorting and formatting happen entirely in your browser. Nothing leaves your device.