Last active
May 1, 2017 10:36
-
-
Save cagcak/82e1e8656525cae680412dfc9ea7d679 to your computer and use it in GitHub Desktop.
This script takes a deformed .json file as argument, then produces formed version.
This file contains hidden or 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
| 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); | |
| }); | |
| }); |
This file contains hidden or 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
| {"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"]}} |
This file contains hidden or 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
| { | |
| "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