Skip to content

Instantly share code, notes, and snippets.

@codesxt
Created May 4, 2023 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codesxt/5e138a2ad9411d92ec65a56a6bdb46c2 to your computer and use it in GitHub Desktop.
Save codesxt/5e138a2ad9411d92ec65a56a6bdb46c2 to your computer and use it in GitHub Desktop.
Sorts a json object by their keys
const fs = require('fs')
const data = require('./data.json')
const sorted = Object.keys(data).sort().reduce(
(obj, key) => {
obj[key] = data[key];
return obj;
},
{}
)
fs.writeFileSync('sorted.json', JSON.stringify(sorted))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment