Skip to content

Instantly share code, notes, and snippets.

@jesperronn
Created February 19, 2017 21:44
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 jesperronn/206dda2539e31debcbd18cf0807c9c0b to your computer and use it in GitHub Desktop.
Save jesperronn/206dda2539e31debcbd18cf0807c9c0b to your computer and use it in GitHub Desktop.
reformat json: read filename and write it again in the same name
#!/usr/bin/env/bash
# reformat json: read filename and write it again in the same name
#
# read JSON file and save it again reformatted
#
# usage:
# `reformat my.json`
reformat(){
node -e "
var fs = require('fs')
fs.readFile('$1', 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
var result = JSON.stringify(JSON.parse(data), null, 2);
//console.log(result);
fs.writeFile('$1', result, 'utf8', function (err) {
if (err) return console.log(err);
});
});
"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment