Created
May 4, 2023 19:05
-
-
Save codesxt/5e138a2ad9411d92ec65a56a6bdb46c2 to your computer and use it in GitHub Desktop.
Sorts a json object by their keys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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