Skip to content

Instantly share code, notes, and snippets.

@emersonbroga
Last active August 29, 2015 14:26
Show Gist options
  • Save emersonbroga/63881f9d8c6f8de94a74 to your computer and use it in GitHub Desktop.
Save emersonbroga/63881f9d8c6f8de94a74 to your computer and use it in GitHub Desktop.
NodeJs and Json Files: Read and Save

#Save on JSON

var fs = require('fs');
var outputFilename = '/tmp/my.json';
var data = {my: 'json'};
fs.writeFile(outputFilename, JSON.stringify(data, null, 4), function(err) {
  if(err) {
   console.log(err);
  } else {
    console.log("JSON saved to " + outputFilename);
  }
});

#Read from JSON

var fs = require('fs');
var path = require('path');
var theaterResponse = JSON.parse(fs.readFileSync(path.resolve('/tmp/my.json'), 'utf8'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment