What does the JSON Sorter do?
The JSON Sorter reorders JSON object keys alphabetically in ascending (A-Z) or descending (Z-A) order in real time. Executing 100% in-browser on blueutils.com, it recursively traverses deeply nested objects, arrays, and dictionaries to produce deterministic, canonical JSON structures. Sorting keys eliminates arbitrary key reordering across serialization runtimes, making Git diffs clean, code reviews readable, and API response caching reliable.
- Real-Time Zero-Latency Key Sorting: Reorders JSON object keys as you type with instant syntax validation and error gutter location.
- Deep Recursive vs. Shallow Traversal: Recursively sorts nested keys across multidimensional hierarchies or sorts only the root object properties.
- Array Preservation: Preserves ordered sequential array elements by default while cleanly sorting objects nested inside array lists.
Core Concepts & Technical Specifications
- Canonical Ordering & Deterministic Hashing:
- Standard JSON specifications define objects as unordered collections of key-value pairs. Normalizing key order alphabetically ensures deterministic payload hashing (e.g. JWT tokens, HMAC signatures) and consistent JSON snapshot comparisons.
- Deep Recursive Traversal:
- Traverses nested object hierarchies down to primitive leaves, sorting object keys at every level while leaving sequential array indices intact.
- In-Browser Privacy:
- All sorting and formatting algorithms execute locally in browser memory.
- No data is transmitted to external servers, logged, or retained.
How to use the tool?
- Input JSON Data:
- Paste your raw JSON payload into the left editor, click Upload to load a local
.jsonfile, or click Sample.
- Paste your raw JSON payload into the left editor, click Upload to load a local
- Configure Options:
- Select your sort order (A-Z (Asc) or Z-A (Desc)), indentation spacing (2 Spaces, 4 Spaces, Tabs, or Minify), and depth (Deep Recursive or Shallow (Root)).
- Copy or Download:
- Sorted JSON appears instantly in the right editor. Click Copy to copy to your clipboard or Download to save as
output.json.
- Sorted JSON appears instantly in the right editor. Click Copy to copy to your clipboard or Download to save as
Pipeline & Contextual Workflows
- Deterministic Git Diffs: Sort schema files and mock datasets before committing to eliminate noisy line changes caused by arbitrary key serialization.
- JSON Formatting & Minification: Format or compress sorted JSON payloads with JSON Formatter & Beautifier or JSON Minifier.
- Structural Diffing: Inspect sorted payloads side-by-side using JSON Diff Tool.
REST API Integration
blueutils.com provides a free REST API endpoint (POST https://blueutils.com/api/json/sorter or POST https://blueutils.com/api/json/sort) for automated CI/CD config generation, Git pre-commit hooks, and data pipelines.
API Request Parameters
| Name | Type | Description | Example |
|---|---|---|---|
rawText / json |
String / Object | Raw JSON string or JSON object payload to sort. | "{\"zebra\":\"animal\",\"apple\":\"fruit\"}" |
order |
String | Sort order: "asc" (default) or "desc". |
"asc" |
recursive |
Boolean | Recursively sort nested sub-objects (default: true). |
true |
indent |
Number / String | Indentation spaces (e.g. 2, 4, or "tab"). Defaults to 2. |
2 |
sortArrays |
Boolean | Optionally sort primitive array elements (default: false). |
false |
API Request Payload Examples
cURL (Using Direct JSON Object)
curl -X POST https://blueutils.com/api/json/sorter \
-H "Content-Type: application/json" \
-d '{
"json": {
"zebra": "animal",
"apple": "fruit",
"banana": "fruit"
},
"order": "asc",
"recursive": true,
"indent": 2
}'Python
import requests
url = "https://blueutils.com/api/json/sorter"
payload = {
"json": {
"zebra": "animal",
"apple": "fruit",
"banana": "fruit"
},
"order": "asc",
"recursive": True,
"indent": 2
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
print(response.json())Java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
String jsonPayload = """
{
"json": {
"zebra": "animal",
"apple": "fruit"
},
"order": "asc",
"recursive": true,
"indent": 2
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://blueutils.com/api/json/sorter"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonPayload))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}API Response Parameters
| Name | Type | Description | Example |
|---|---|---|---|
isValid |
Boolean | Indicates whether the sort operation succeeded. | true |
result |
String | Sorted and formatted JSON string output. | "{\n \"apple\": \"fruit\",\n \"zebra\": \"animal\"\n}" |
data |
Object / Array | Parsed JavaScript object/array representation with sorted keys. | {"apple":"fruit","zebra":"animal"} |
originalSize |
Number | Byte size of raw input payload in UTF-8. | 45 |
resultSize |
Number | Byte size of sorted output JSON in UTF-8. | 52 |
keysSortedCount |
Number | Total number of object keys processed across all levels. | 2 |
error |
String | Detailed error explanation returned on invalid syntax. | "Invalid JSON syntax: Unexpected token '}' (Line 2)" |
API Response Payload Examples
Success Response (HTTP 200 OK)
{
"isValid": true,
"message": "JSON keys sorted in ascending order successfully.",
"result": "{\n \"apple\": \"fruit\",\n \"banana\": \"fruit\",\n \"zebra\": \"animal\"\n}",
"data": {
"apple": "fruit",
"banana": "fruit",
"zebra": "animal"
},
"order": "asc",
"recursive": true,
"originalSize": 45,
"resultSize": 52,
"nodeCount": 3,
"keysSortedCount": 3
}Validation Failure Response (HTTP 400 Bad Request)
{
"isValid": false,
"error": "Invalid JSON syntax: Unexpected token '}' at position 15 (Line 1, Column 16)"
}Rate Limit Exceeded Response (HTTP 429 Too Many Requests)
{
"error": "API rate limit exceeded. Please wait or contact support@blueutils.com."
}Why use an API to sort JSON?
Automating JSON key sorting improves version control stability:
- Clean Git Diffs: Eliminates noisy line changes in CI/CD configuration files caused by unordered serialization.
- Deterministic Token Hashing: Ensures identical payload hashes when verifying digital signatures, JWTs, or cache keys.
- LLM Context Optimization: Normalizes object structures before feeding them into prompt contexts for faster parsing.
Native Usage
Sort JSON keys locally across terminal environments and programming runtimes:
Linux / macOS (jq)
# Sort all keys alphabetically in JSON using jq
jq -S . input.json > output.jsonWindows (PowerShell)
# Read JSON, sort properties recursively, and write formatted output
$json = Get-Content -Raw -Path .\input.json | ConvertFrom-Json
function Sort-JsonObject {
param($InputObject)
if ($InputObject -is [System.Management.Automation.PSCustomObject]) {
$sorted = [ordered]@{}
$InputObject.PSObject.Properties.Name | Sort-Object | ForEach-Object {
$sorted[$_] = Sort-JsonObject $InputObject.$_
}
return [PSCustomObject]$sorted
} elseif ($InputObject -is [System.Collections.IList]) {
return @($InputObject | ForEach-Object { Sort-JsonObject $_ })
}
return $InputObject
}
(Sort-JsonObject $json) | ConvertTo-Json -Depth 10 | Set-Content .\output.jsonPython
import json
raw_json = '{"zebra": "animal", "banana": "fruit", "apple": "fruit"}'
parsed = json.loads(raw_json)
print(json.dumps(parsed, sort_keys=True, indent=2))Java (Jackson / TreeMap)
import java.util.Map;
import java.util.TreeMap;
public class Main {
public static Map<String, Object> sortMapRecursively(Map<String, Object> map) {
Map<String, Object> sorted = new TreeMap<>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
if (entry.getValue() instanceof Map) {
sorted.put(entry.getKey(), sortMapRecursively((Map<String, Object>) entry.getValue()));
} else {
sorted.put(entry.getKey(), entry.getValue());
}
}
return sorted;
}
}