JSON Sorter
Sort JSON into repeatable key order in your browser, clean JSONC drafts, flag duplicate names, and review paths before copying output.{{ sortedJson || sortedFallbackText }}
| Check | Value | Detail | Copy |
|---|---|---|---|
| {{ row.check }} | {{ row.value }} | {{ row.detail }} |
| Path | Change | Before | After | Detail | Copy |
|---|---|---|---|---|---|
{{ row.path }} |
{{ row.change }} | {{ row.before }} | {{ row.after }} | {{ row.detail }} | |
| The selected order rules did not change any object or array order. | |||||
| Path | Pointer | Type | Depth | Preview | Copy |
|---|---|---|---|---|---|
{{ row.path }} |
{{ row.pointer }} |
{{ row.type }} | {{ row.depth }} | {{ row.preview }} | |
| Sort a valid JSON document to populate the path ledger. | |||||
{{ resultJson }}
Introduction:
JSON is a data format first and a text file second. Machines parse objects, arrays, strings, numbers, booleans, and null, but people still review JSON in pull requests, support tickets, deployment settings, API examples, saved responses, and test fixtures. When object keys drift into a new order on every generation, a diff can look noisy even when the parsed value barely changed.
Sorting JSON is useful only when the reader understands which order is cosmetic and which order is data. Object member names are commonly treated as an unordered set for interchange, so alphabetizing keys can make review easier. Array items are different because their sequence usually carries meaning. Reordering a list of routes, permissions, tasks, or pricing tiers can change the document even if every item is still present.
- Object keys
- Usually safe to reorder for readability or repeatable diffs when the receiving system does not require a special canonical form.
- Arrays
- Should stay in source order unless each item is a record and a field such as
id,name,priority, orrankdefines the intended sequence. - Duplicate names
- Need review before sorting because a normal parser may keep only one value and hide the ambiguity.
Ordering rules should match the job. Unicode code-unit order is a repeatable text comparison that works well for fixtures, generated examples, and diff review. Case-insensitive and natural number-aware ordering are easier to scan when keys look like labels, but they are reading aids rather than a formal canonical JSON rule.
Strict JSON and JSON with comments have different roles. Strict JSON is the safer interchange target because standard JSON parsers should understand it. Commented configuration drafts are convenient while editing, but comments and trailing commas are not part of the standard JSON value. Cleaning that text means draft-only notes disappear and the final document becomes ordinary JSON.
A sorted document can still be wrong. Duplicate object names are the most common false-confidence trap because different receivers may keep the last value, reject the object, or expose duplicates differently. Number precision, string escaping, array order, and a receiving system's own canonicalization requirements can also matter when JSON feeds signatures, caches, test snapshots, or deployment automation.
How to Use This Tool:
Start with the parser and ordering choices, then use the audit views before copying the sorted output.
- Choose Parser mode. Use Strict JSON for normal interchange data. Use JSONC cleanup only when a draft file contains JavaScript-style comments or trailing commas that should be removed before sorting.
- Add the input in JSON source. You can paste one JSON value, browse for a
.json,.jsonc, or.txtfile, drop one file onto the editor, or load the sample. Browser file loading is limited to files smaller than 2 MB. - Set Key order, Sort scope, and Key comparison. For stable diffs, keep A-Z keys, Recursive object keys, and Unicode code-unit order.
- Leave Array handling on Preserve array order unless the array is a record list with a real sort field. If you enable array sorting, enter a plain property or dot path such as
id,priority, ormeta.rank. - Pick the Output format. The available formats are 2-space JSON, 4-space JSON, tab-indented JSON, compact JSON, and a custom 0 to 10 space indent. Turn on End newline when a downloaded file should finish with one final line break.
- Open Advanced when duplicate names or long path tables matter. Warn and sort reports repeated names, Block sorted output withholds sorted text until duplicates are fixed, and Path ledger preview controls how many rows are shown in the table.
- Review Sorted JSON, Sort Audit, Order Changes, Path Ledger, Key Depth Chart, and JSON. Copy or download only after the parse status, duplicate notes, and changed-order rows match what you expected.
If the summary says the input needs attention, fix the parser message first. If duplicate keys are blocked, rename or remove the repeated member before relying on the sorted output.
Interpreting Results:
Sorted JSON is the parsed value written back with the selected key order, array policy, indentation, and final newline setting. A result with zero changed objects is still useful because it confirms the source was already in the selected order.
Sort Audit is the confidence check. It reports whether parsing succeeded, which key comparison was used, how many objects were scanned and changed, whether any arrays were reordered, the input and output size, cleanup notes, and duplicate-key details when duplicate notes are enabled.
Order Changes lists only paths where visible order changed. Object rows show the key sequence before and after sorting. Array rows appear only when array sorting by property is enabled and at least one eligible array actually changes order.
Path Ledger lists the sorted structure as readable paths and JSON Pointer values, with the value type, depth, and a short preview. Use it to confirm that nested data still has the shape you expected after cleanup and sorting.
Key Depth Chart groups keys, objects, and arrays by nesting depth. It is a structure check, not a quality score: a deep chart can be normal for API responses, but unexpected depth often points to a wrapper, escaped JSON string, or nested payload you should inspect.
The JSON tab is a machine-readable report containing the selected settings, metrics, duplicate-key notes, order rows, chart rows, and sorted text. Use it when you want to save the review evidence along with the formatted output.
Technical Details:
JSON sorting begins with the distinction between a text document and a parsed value. Whitespace, indentation, and source formatting are text features. Objects, arrays, strings, numbers, booleans, and null are value features. A sorter must parse the text before it can safely reorder object members, then serialize the value back to text after the chosen ordering rules are applied.
Object member names should be unique for interoperability. When the same name appears twice in one object, the JSON text has already become ambiguous for many receivers. Sorting cannot recover a value that a normal parser would discard, so duplicate-name review has to happen before the sorted text is trusted.
Transformation Core:
| Stage | Mechanism | Important Check |
|---|---|---|
| Prepare text | Strict mode parses standard JSON as written. JSONC cleanup removes comments and trailing commas before parsing. | Cleanup notes mean draft-only text was removed, and the final output is standard JSON. |
| Parse one value | The input must resolve to one JSON value: object, array, string, number, boolean, or null. | Syntax errors stop sorting and report the parser message, with line and column when available. |
| Scan repeated names | Object member names are checked in the source text before parsing can collapse repeated names. | Warn, block, or ignore behavior depends on the duplicate key policy. |
| Sort object members | Eligible object keys are sorted A-Z or Z-A with the selected comparison rule. | Objects scanned and objects changed are counted separately. |
| Handle arrays | Arrays keep their original sequence unless property sorting is enabled for the root array or all object arrays. | Missing or null sort values move after matched values, and ties keep original order. |
| Serialize text | The sorted value is written as compact JSON, spaces, tabs, or a custom indent capped at 10 spaces. | Source whitespace is not preserved; output whitespace is regenerated. |
| Walk the structure | The sorted value is counted by nodes, objects, arrays, scalars, keys, and depth. | Path rows and depth chart data come from the sorted value. |
Ordering Rules:
| Choice | What It Does | Use Case |
|---|---|---|
| Unicode code-unit order | Compares key strings by raw code-unit values, then applies A-Z or Z-A direction. | Repeatable diffs, fixtures, generated examples, and signature-adjacent review. |
| Case-insensitive order | Compares keys without case sensitivity first, then uses the original string as a tie-breaker. | Documents where human readers expect Name and name to sit together. |
| Natural number-aware order | Compares embedded numbers as numbers where possible, so item2 can come before item10. | Generated labels, numbered fields, and review documents meant for people. |
| Recursive object keys | Sorts every object found at any depth, including objects inside arrays. | Whole-document normalization for nested configuration or API payloads. |
| Top-level object only | Sorts only the root object when the root value is an object. | Examples where the outer wrapper should be tidy but nested member order should remain as written. |
| Property array sorting | Sorts eligible arrays of objects by a plain property or dot path. | Record sets where a chosen field truly defines the intended order. |
Path and Pointer Evidence:
| Ledger Field | Meaning | Use It For |
|---|---|---|
| Path | A readable dot or bracket path such as $.alpha.channels[0].name. | Finding nested values while reviewing formatted output. |
| Pointer | A JSON Pointer value where each token is prefixed by /, with ~ encoded as ~0 and / encoded as ~1. | Matching locations reported by systems that use pointer-style paths. |
| Type | The JSON value kind at that location: object, array, string, number, boolean, or null. | Checking whether cleanup or parsing changed the expected shape. |
| Depth | The nesting level from the root value. | Spotting unexpectedly deep wrappers or embedded payloads. |
| Preview | A compact summary of the value, such as object keys, array length, or short scalar text. | Confirming that the path points to the intended part of the document. |
Alphabetized JSON is not automatically canonical JSON. A formal canonicalization scheme can also define number serialization, string escaping, Unicode handling, whitespace, duplicate-name rejection, and exact property ordering. Match the receiving system's rule set before using sorted JSON for signatures, cache keys, or protocol-level equality checks.
Privacy Notes:
Pasted text and browsed or dropped files are read, parsed, sorted, copied, charted, and downloaded in the browser during the current session. Sorting does not need a server lookup. The page may load its charting library from a public CDN, but your JSON text and local file contents are not submitted for sorting.
Advanced Tips:
- Use Unicode code-unit order for repeatable fixtures, generated examples, and signature-adjacent review; use natural or case-insensitive comparison only when human scanning matters more than strict repeatability.
- Keep Array handling on Preserve array order unless a record field or dot path truly defines sequence for the list.
- Use Block sorted output when duplicate object names would make a handoff unsafe, especially before deployment or test snapshot updates.
- Set Path ledger preview lower for very large documents so the rendered table stays readable while the JSON report still keeps full metrics.
- Turn on End newline for files that will be committed to a repository, because many editors and linters expect the final line break.
- Check Order Changes before copying output; it should show only the object or array locations you meant to reorder.
Worked Examples:
An API configuration sample starts with keys in the order zebra, alpha, release, and limits. With A-Z recursive sorting and Unicode code-unit comparison, the sorted output places object members in repeatable order at every depth, and the audit reports how many objects were scanned and changed.
A documentation example needs only its wrapper keys cleaned up. Select top-level sorting so the root object changes while nested example objects keep their original member order. The path ledger can still be used to confirm that nested values are present.
A root array contains records with numeric rank values. Enable root-array property sorting and set the property to rank. Records with matching rank values sort by that number, ties keep their original order, and records without a value sort after matched records.
A source object contains {"id":1,"id":2}. Warning mode lists the duplicate and still produces sorted text from the parsed value. Blocking mode withholds sorted output until the repeated name is removed or renamed.
FAQ:
Does sorting JSON keys change the data?
Sorting object member names changes visible text order after parsing, not the values themselves. Sorting arrays is different because array sequence is data, so keep array order unless the array is a record set with a real sort field.
Why did comments disappear from my output?
JSONC cleanup removes comments and trailing commas before parsing, then writes standard JSON. Comments and draft-only commas are not part of the final JSON value.
Which key comparison should I use for repeatable diffs?
Use Unicode code-unit order with the same A-Z or Z-A direction every time. Case-insensitive and natural ordering are easier to read in some files, but they are not strict canonicalization rules.
Why does a duplicate-key warning matter?
A repeated name inside one object can be interpreted differently by different receivers. The sorted text may show only one of the values, so fix duplicates before using the output for handoff or automation.
What should I do when parsing fails?
Read the invalid-input message, fix the reported syntax issue, and try again. If the text is a commented configuration draft, switch to JSONC cleanup only when comments or trailing commas are the expected reason strict JSON fails.
Does the sorter send my JSON anywhere?
The JSON text and selected files are handled in the browser for sorting, path review, charting, copying, and downloads. The sorting step does not require uploading the document.
Glossary:
- Object member
- A JSON object name and value pair, often called a key and value in everyday use.
- Array order
- The sequence of items inside a JSON array. Unlike object member order, array order is normally part of the data.
- Unicode code-unit order
- A deterministic string comparison based on raw code-unit values rather than human language collation.
- JSONC
- JSON with comments, commonly used for configuration drafts that allow comments and sometimes trailing commas.
- Duplicate key
- A repeated object member name inside the same object.
- JSON Pointer
- A slash-prefixed path syntax for identifying a value inside a JSON document.
- Path Ledger
- The result table that lists paths, pointers, value types, depth, and previews for the sorted structure.
- Key Depth Chart
- The chart that groups keys, objects, and arrays by nesting depth.
References:
- RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format, RFC Editor, December 2017.
- RFC 8785: JSON Canonicalization Scheme (JCS), RFC Editor, June 2020.
- RFC 6901: JavaScript Object Notation (JSON) Pointer, RFC Editor, April 2013.
- JSONC Specification, JSONC.org.
- JSON.stringify(), MDN Web Docs.