Reputation: 1882
I have a data set in data.json
file. I need to add 'uuid'
field to each record. The project is in Node.js.
I can read the file by using
module.exports.api = function(server, fs) {
// Sample Rest Call
server.get('/api/getData', function(req, res) {
fs.readFile(__dirname + '/data.json', function(err, data) {
if (err) throw err;
res.send(200, JSON.parse(data));
});
});
};
I have the option to use mongoose as well.
Upvotes: 0
Views: 1623
Reputation: 4332
This is by no means 100% efficient, there are of course better solutions. But you could manipulate the data returned from the JSON by looping through it and setting the index as the UUID of the current object being looped through. Like this
module.exports.api = function(server, fs) {
// Sample Rest Call
server.get('/api/getData', function(req, res) {
fs.readFile(__dirname + '/data.json', function(err, data) {
if (err) throw err;
let results = JSON.parse(data);
let resultsWithUUID = [];
result.forEach((res, index) => {
res.uuid = index;
resultsWithUUID.concat([res]);
}
res.send(200, resultsWithUUID);
});
});
};
Upvotes: 1