Skip to content

Instantly share code, notes, and snippets.

@mixu
Created October 3, 2014 00:06
Show Gist options
  • Save mixu/5a25715672da2b4f199a to your computer and use it in GitHub Desktop.
Save mixu/5a25715672da2b4f199a to your computer and use it in GitHub Desktop.
Alphabetize JSON input
find ./conf -type f -name "*.json" -print | xargs -L 1 node -e "var out = '', fs = require('fs'); var out = process.argv[process.argv.length - 1]; var j = JSON.parse(fs.readFileSync(out).toString()); fs.writeFileSync(out, JSON.stringify(traverse(j), null, 2)); function traverse(j) { if (Array.isArray(j)) { return j.map(traverse); } if (typeof j !== 'object' || j === null) { return j; } var r = {}; Object.keys(j).sort().forEach(function(k) { r[k] = traverse(j[k]); }); return r; }"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment