Sort JSON Keys for a Focused Object Comparison

Use recursive key sorting to compare two fictional JSON samples while accounting for the limitations of text-based diffs.

By DevSphere Editorial · · 5 min read

Open JSON Beautifier

Know when sorting helps

Two JSON objects can hold the same members in different textual orders, creating a noisy diff when a generator emits keys inconsistently. JSON Beautifier can sort object keys alphabetically, including nested objects, before producing its output. This can make a sample comparison easier to scan when key order is not meaningful to the application. It does not compare two inputs, calculate a semantic diff, or know whether a downstream text processor depends on order. Use it only after confirming the conventions for the relevant format and consumer. We will sort a fictional product record with nested dimensions and then compare it with a second record separately. The sample contains no real catalog identifiers. Keep an untouched source for each sample because formatting and sorting change their serialized order, even though sorting does not intentionally change field values.

{"name":"Demo Tray","dimensions":{"width":12,"height":4},"active":true,"code":"TRAY-8"}

Produce the normalized view

Paste the first object into the Input JSON editor and enable Sort Keys Alphabetically. Keep Standard indentation and double quotes. The Beautified Output should place active before code, dimensions, and name in alphabetical order, and sort width and height within dimensions according to the formatter's locale comparison. The exact visual order is not a substitute for checking values. Paste the second sample only after copying or preserving the first output; the component has a single input and does not retain a comparison pane. Apply the same settings to each sample, then place the outputs next to each other in your trusted diff tool. If the normalized serialization is identical, the samples may be equivalent at a shallow visual level, but verify values and types explicitly. The Beautifier cannot distinguish a changed number from a changed string on its own.

Separate ordering from data changes

A consistent order reduces noise only when the objects are otherwise comparable. If one sample adds a field, removes a nested member, changes 12 to 13, or changes true to the string "true", the normalized view should make that difference easier to spot. Look at dimensions.height and dimensions.width in context, not merely line changes. Sorting keys recursively does not reorder arrays because an array's position is meaningful, and it does not merge different schemas. In many JSON parsers object member order is not semantically significant, but downstream systems or signatures can impose their own constraints. A canonical JSON protocol has stricter rules than this UI toggle. Do not sort a signed request body and expect the signature to remain valid. For signatures, use the specified canonicalization algorithm and preserve the original bytes as required.

{
  "active": true,
  "code": "TRAY-8",
  "dimensions": {"height": 4, "width": 12},
  "name": "Demo Tray"
}

Respect serializer constraints

Sort Keys works with valid JSON parsed by the browser. Comments, single quotes, trailing commas, and unquoted properties will fail before output is produced. The formatter can also choose tabs, different space indentation, inline arrays, inline short objects, quote style, and trailing commas; keep these settings identical for every sample or formatting noise can return. Single quotes and trailing commas are not valid strict JSON, so avoid those options when the output will be parsed as JSON. The custom serializer is a convenience formatter rather than an implementation of a full canonical JSON specification. Locale-aware ordering may not match another language's sorting rules for unusual Unicode property names. If a byte-exact or cryptographic comparison is required, use the official implementation instead. For ordinary review, record the chosen preset and verify one nested object and one array to ensure the output is being interpreted correctly.

Confirm the comparison independently

If the output is absent, check validity status and inspect the first malformed key or string. If a key seems missing, confirm the second sample actually contains it and remember that the page accepts only one input at a time. Apply the same options to each sample, then compare the sorted outputs and check the values and types from the original sources. For the fictional record, confirm name Demo Tray, dimensions width 12 and height 4, active true, and code TRAY-8. Character counts can reveal that formatting changed length, but they do not prove equality. Keep object ordering separate from array ordering in the review. The utility has no side-by-side diff and cannot certify that two parsed objects are semantically identical; use a structured diff or test for important comparisons. Preserve originals, avoid sensitive inputs, and clear the workspace once the review is complete.

Sorting is less helpful when the order itself communicates intent, such as a hand-authored sequence of steps inside an array or a carefully grouped configuration file. This control only sorts object keys; array positions remain in their original order. If a diff still shows a change after normalization, check whether an array item moved or a scalar changed before concluding the object is different. Conversely, identical sorted text does not test hidden application defaults or values omitted from both samples. For reproducible comparison, store the exact source versions, apply one formatting convention, and use a structured JSON diff that compares parsed values. Use this page as a preparation step for human review, not as the final evidence for a cryptographic signature or a data migration.

Browse all tutorials