Blog
Wild & Free Tools

Sort JSON Keys With jq on the Command Line — And the Browser Alternative That Needs No Install

Last updated: April 2026 5 min read
Quick Answer

Table of Contents

  1. jq sort-keys
  2. When jq wins
  3. When browser wins
  4. Practical tips
  5. Frequently Asked Questions

jq has a built-in flag for sorting JSON keys alphabetically: jq --sort-keys . or the shorter jq -S .. It sorts recursively at every nesting level. One flag, done. For CLI workflows this is perfect. For JSON that is not already on your terminal — in a browser tab, an email, a clipboard — our JSON Key Sorter is faster than saving to a file and running jq.

The jq --sort-keys Flag

The basic command:

jq --sort-keys . input.json

Or the short form:

jq -S . input.json

Both sort keys alphabetically at every level. Recursion is automatic — nested objects get sorted too. Output is pretty-printed by default.

For compact (one-line) sorted output, combine with -c:

jq -Sc . input.json

To read from stdin (pipe-friendly):

curl -s https://api.example.com/data | jq -S .

To write back to the same file (careful — jq cannot safely read and write the same file, so use a temp):

jq -S . input.json > tmp.json && mv tmp.json input.json

When jq Is the Right Tool

Already piping through commands. If your JSON is the output of curl, docker inspect, kubectl get -o json, or any other CLI command, piping to jq -S is one character shorter than pasting into a browser.

Scripts and automation. Anything that runs unattended needs jq or similar. Browser tools are interactive-only.

Servers you SSH into. No browser available. jq is usually pre-installed on Linux. Windows Server, you may need to install it first.

Sorting many files. A shell loop with jq handles hundreds of files in seconds:

for f in configs/*.json; do
  jq -S . "$f" > tmp && mv tmp "$f"
done

Doing this in a browser would be one-file-at-a-time and take hours.

Sell Custom Apparel — We Handle Printing & Free Shipping

When the Browser Tool Is Faster

Machine without jq installed. Fresh MacBook, Windows desktop, borrowed machine. Installing jq is friction; opening a browser is not.

JSON already in a browser tab. You are looking at an API response in DevTools. Copy, paste into the sorter, read. No round trip through a terminal.

Pairing with sort-and-compare. Sort both payloads in the browser, paste both into our diff checker, see differences. All tabs stay in one tool ecosystem.

Non-developer stakeholders. Asking a PM or designer to install jq is unreasonable. They can use a web tool.

Quick triage. A junior engineer wants to see a payload in alphabetical order. Browser tool is discoverable; jq --sort-keys is documented knowledge.

Practical Tips for jq Sort Users

Pre-commit hook for jq sort. If you do not want to install Prettier's plugin system, a simpler pre-commit script:

#!/bin/bash
for f in $(git diff --cached --name-only --diff-filter=ACM | grep '\.json$'); do
  jq -S . "$f" > "$f.tmp" && mv "$f.tmp" "$f"
  git add "$f"
done

Put this in .git/hooks/pre-commit. Now every commit auto-sorts JSON files.

Sort and minify in one pass. jq -Sc . produces sorted one-line output. Useful for compact canonical form.

Sort but preserve top-level order. jq does not have this built in. For package.json style conventional ordering, use the sort-package-json npm package instead. Our browser tool also does full-sort; for selective top-level preservation, manual editing after the sort is required.

Windows jq alternatives. If jq is hard to get on Windows, PowerShell has ConvertFrom-Json | ConvertTo-Json -Depth 100 which alphabetizes properties in recent PowerShell versions. Browser tool works identically on Windows though, making it the easier option.

Sort JSON Without jq Installed

Free browser tool — same recursive alphabetical sort, no install needed.

Open Free JSON Key Sorter

Frequently Asked Questions

Does jq -S and jq --sort-keys produce the same output?

Yes, identical. -S is the short flag, --sort-keys is the long form. Both sort alphabetically and recursively. Use whichever is more readable in your script.

Can jq sort JSON arrays?

The --sort-keys flag only sorts object keys, not array elements. To sort an array by a value, use a jq filter: jq sort_by(.name) or jq . sort_by(.name). Our browser tool also only sorts object keys, not array elements.

Does jq sort BigQuery or Snowflake JSON output?

Yes — if you pipe SELECT output to jq -S, it sorts the JSON representation. For in-warehouse canonical sort, use the database native functions. See our SQL flatten and warehouse flatten guides.

Is there a jq equivalent for reverse-alphabetical sort?

Not directly. --sort-keys is ascending only. For descending, pipe to a second jq filter that reverses each object: jq -S . input.json | jq to_entries | map(.) | sort_by(.key) | reverse | from_entries. Awkward. The browser tool has a Z-A toggle that does this cleanly.

Lauren Mitchell
Lauren Mitchell Privacy & Security Writer

Lauren spent four years as an IT security analyst translating complex security concepts for everyday users.

More articles by Lauren →
Launch Your Own Clothing Brand — No Inventory, No Risk