How to store data to JSON file from node js . If that json file already having some data. How to add data to json file in append mode....?
Asked
Active
Viewed 1,483 times
-3
-
I downvote because this question doesn't show any research effort : http://idownvotedbecau.se/noresearch/ . I think your question is actually describing a database. – Mickael Aug 03 '18 at 15:40
-
2Possible duplicate of [write/add data in JSON file using node.js](https://stackoverflow.com/questions/36856232/write-add-data-in-json-file-using-node-js) – Kevin B Aug 03 '18 at 15:50
-
@MickaëlB - I don't see this question describing a database. I see it talking about JSON in a local file. But yes, this question is too broad, since there's no code shown, no sample JSON, no expected output, no actual output, etc. Also, please stop using that `idownvoted` site - it's passive-aggressive at best. I look forward to the day the Stack Exchange network bans its use, just as it banned `let me google that for you`. – David Makogon Aug 05 '18 at 15:44
-
@DavidMakogon My point is that it would probably be better use a database if he has data that have to be updated instead of a storing into a JSON file. Also how is `idownvoted` site is different of [How do I ask a good question](https://stackoverflow.com/help/how-to-ask) ? – Mickael Aug 06 '18 at 08:24
1 Answers
-1
This module will help you store data to JSON file(s) (create new) and also update it:
const fs = require('fs');
var lib = {};
// Create new json file
lib.create = function(dir,file,data,callback){
// Open the file for writing
fs.open(dir+'/'+file+'.json', 'wx', function(err, fileDescriptor){
if(!err && fileDescriptor){
// Convert data to string
var stringData = JSON.stringify(data);
// Write to file and close it
fs.writeFile(fileDescriptor, stringData,function(err){
if(!err){
fs.close(fileDescriptor,function(err){
if(!err){
callback(false);
} else {
callback('Error closing new file');
}
});
} else {
callback('Error writing to new file');
}
});
} else {
callback('Could not create new file, it may already exist');
}
});
};
// Update data in a json file
lib.update = function(dir,file,data,callback){
// Open the file for writing
fs.open(dir+'/'+file+'.json', 'r+', function(err, fileDescriptor){
if(!err && fileDescriptor){
// Convert data to string
var stringData = JSON.stringify(data);
// Truncate the file
fs.ftruncate(fileDescriptor,function(err){
if(!err){
// Write to file and close it
fs.writeFile(fileDescriptor, stringData,function(err){
if(!err){
fs.close(fileDescriptor,function(err){
if(!err){
callback(false);
} else {
callback('Error closing existing file');
}
});
} else {
callback('Error writing to existing file');
}
});
} else {
callback('Error truncating file');
}
});
} else {
callback('Could not open file for updating, it may not exist yet');
}
});
};
module.exports = lib;
Store the above code to new js file and save it for example as data.js on same folder which contain your js file, after that import it to your code file to can use:
const data = require('./data.js')
// Store JSON to file Example:
var jData = {'x':1, 'y':2};
data.create('./data','test1',jData ,function(err){ if(!err){console.log('file created successfully');} else{console.log(err)} });
// Update same JSON file Example:
jData.y = 5;
data.update('./data','test1',jData ,function(err){ if(!err){console.log('file updated successfully');} else{console.log(err)} });
Note: you will need to create 'data' folder which will contain json files or change dir parameter value to any existing folder.

Ezzat Elbadrawy
- 142
- 1
- 3