How can i read a Json file with a Azure function-Node.js
Here's a working example that uses the built in fs
module:
var fs = require('fs');
module.exports = function (context, input) {
var path = __dirname + '//test.json';
fs.readFile(path, 'utf8', function (err, data) {
if (err) {
context.log.error(err);
context.done(err);
}
var result = JSON.parse(data);
context.log(result.name);
context.done();
});
}
Note the use of __dirname
to get the current working directory.
There is a quicker way than @mathewc's. NodeJS allows you to require
json files directly without the explicit read -> parse steps nor without an async callback. So:
var result = require(__dirname + '//test.json');