Blogchevron_rightJSON Tools
JSON Tools

How to Sort JSON Object Keys in JavaScript and Node.js

JavaScript has no built-in JSON key sorting, but several reliable approaches provide this functionality. Here is how to sort JSON keys correctly, including for nested objects.

April 18, 2026·7 min read

The JSON.stringify Key Sort Trick

JSON.stringify has an undocumented but reliable behavior: when the replacer parameter is null (or omitted), keys are stringified in insertion order. However, when you pass an object through JSON.parse first and then use a replacer array, you get control over the output.

A simpler approach: sort the object recursively and let JSON.stringify serialize it in the new order. The key insight is that modern JavaScript engines preserve insertion order for string keys, so sorting the keys and rebuilding the object produces a sorted JSON string when stringified.

Recursive Deep Sort Function

A recursive sort function handles nested objects at all levels: function sortKeys(obj) { if (Array.isArray(obj)) return obj.map(sortKeys); if (obj !== null && typeof obj === "object") { return Object.keys(obj).sort().reduce((acc, k) => { acc[k] = sortKeys(obj[k]); return acc; }, {}); } return obj; }. Call sortKeys(data) before JSON.stringify(data) to get sorted output.

This function handles all JSON types correctly: arrays are preserved in their original order (array element order is semantically significant), primitive values are returned unchanged, and object keys are sorted recursively at every nesting level.

Sorting in Node.js File Processing

To sort all keys in a JSON file: const fs = require("fs"); const data = JSON.parse(fs.readFileSync("input.json", "utf8")); const sorted = sortKeys(data); fs.writeFileSync("output.json", JSON.stringify(sorted, null, 2)). This reads the file, sorts keys, and writes the sorted formatted version.

For a command-line JSON sorter in Node.js: node -e "const d=JSON.parse(require('fs').readFileSync(process.argv[1])); const s=k=>Array.isArray(k)?k.map(s):k&&typeof k==='object'?Object.keys(k).sort().reduce((a,j)=>{a[j]=s(k[j]);return a},{} ):k; console.log(JSON.stringify(s(d),null,2))" input.json

Sorting with jq

For command-line sorting, jq provides the simplest approach. jq -S . input.json sorts all object keys alphabetically and outputs formatted JSON. The -S flag enables key sorting; combine with -c for compact output: jq -Sc . input.json.

jq sort is recursive — it sorts keys at every nesting level of the JSON document. It is the most reliable and readable option for command-line JSON key sorting, especially in shell scripts and CI pipelines.

Try JSON Sorter Free Online

No sign-up required. 100% client-side — your data never leaves your browser.

Open JSON Sorterarrow_forward

Frequently Asked Questions

Does JavaScript guarantee object key order?

Yes, as of ES2015. Integer keys come first (in numeric order), then string keys in insertion order, then Symbol keys. Sorting and rebuilding an object produces sorted insertion order that is preserved by JSON.stringify.

Is there a npm package for JSON key sorting?

Yes. sort-keys, sort-keys-recursive, and json-stable-stringify all provide this functionality. json-stable-stringify is particularly useful because it produces deterministic JSON output for hashing.

Does jq -S sort array elements?

No. jq -S sorts only object keys, not array elements. Array element order is preserved. To sort array elements, use jq "sort_by(.field)" for arrays of objects or jq "sort" for arrays of primitives.

How to Sort JSON Object Keys in JavaScript and Node.js