Skip to content

Instantly share code, notes, and snippets.

@Fishrock123
Created December 23, 2013 17:51
Show Gist options
  • Select an option

  • Save Fishrock123/8101578 to your computer and use it in GitHub Desktop.

Select an option

Save Fishrock123/8101578 to your computer and use it in GitHub Desktop.
Sorts a JSON Object file alphabetically.
var specs = require('./Your_Json_File_Here.json')
, out = {}
, fs = require('fs')
function iterateObjectAlphabetically(obj, callback) {
var arr = []
, i
for (i in obj) {
if (obj.hasOwnProperty(i)) {
arr.push(i);
}
}
arr.sort()
if (callback) {
callback(obj, arr)
}
}
iterateObjectAlphabetically(specs, function(obj, arr) {
console.log(arr)
for (var i = 0; i < arr.length; i++) {
out[arr[i]] = specs[arr[i]]
}
fs.writeFile('./Your_Object_Alphabetized_Json_File_Here.json', JSON.stringify(out, null, 2), function(err) {
if (err) {
console.log(err)
} else {
console.log("Alphabetized JSON.")
}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment