A simple nodejs wrapper for inserting and deleting objects into a json file. Uses fs library to read/write the json file.
-
Clone/Download zip of repository.
-
Extract content of the zip.
-
Open cmd to run the following code -
cd "folderpath where zip is extracted"
npm install
- After the installation, run -
npm start
- Import the file in your project -
var jsonOperation = require('./json-read-write.js');
- Insert object in the json using
var object =
{
title: "Installing Packages",
body: "Run npm install to install the packages. "
};
jsonOperation.insertJsonObject(object);
use the following to create a promise function to print json after inserting an object -
var object =
{
title: "Installing Packages",
body: "Run npm install to install the packages. "
};
var insertAndPrintFunction = function(){
var promise = new Promise(function(resolve, reject){
jsonOperation.insertJsonObject(object);
resolve();
});
return promise;
}
insertAndPrintFunction().then(function(){
console.log("Printing the contents of JSON file: \n");
jsonOperation.printJsonFile();
});
{
"objectarray": [
{
"title": "Installing Packages",
"body": "Run npm install to install the packages. "
}
]
}
Refer to the line in the js file - objectarray is the array name defined in the json file.
fileAsJson.objectarray.push(object); //objectarray is the array defined in your json.