Skip to content

Instantly share code, notes, and snippets.

@cagcak
Last active May 1, 2017 10:36
This script takes a deformed .json file as argument, then produces formed version.
var fs = require("fs");
var input = process.argv.slice(2).toString();
// USAGE:
// node 'script' 'target'
// (ex: node A-json-formatter.js a:\source\nodejs\formed.json)
fs.open(input, 'r', (err, fd) => {
if (err) {
if (err.code === 'ENOENT') {
console.error('myfile does not exist');
return;
}
throw err;
}
fs.readFile(fd, 'utf8', function (err,deformed) {
if (err) {
return console.log(err);
}
var parser = JSON.parse(deformed);
var formed = JSON.stringify(parser, undefined, 4);
fs.writeFile('formed.json', formed, function(err) {
if(err) {
console.log(err);
} else {
console.log("JSON saved to " + 'formed.json' + " in "+ __dirname);
}
});
// console.log("Deformed input JSON data:\n\n " + deformed);
});
});
{"installed":{"client_id":"CLIENT_ID.apps.googleusercontent.com","project_id":"tubenode-v0","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://accounts.google.com/o/oauth2/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"CLIENT_SECRET","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}}
{
"installed": {
"client_id": "CLIENT_ID.apps.googleusercontent.com",
"project_id": "tubenode-v0",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_secret": "CLIENT_SECRET",
"redirect_uris": [
"urn:ietf:wg:oauth:2.0:oob",
"http://localhost"
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment