Skip to content

prav10194/nodejs-fs-json-wrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nodejs-fs-json-wrapper

A simple nodejs wrapper for inserting and deleting objects into a json file. Uses fs library to read/write the json file.

Running the example.js -

  1. Clone/Download zip of repository.

  2. Extract content of the zip.

  3. Open cmd to run the following code -

cd "folderpath where zip is extracted"
npm install
  1. After the installation, run -
npm start

Usage -

  1. Import the file in your project -
var jsonOperation = require('./json-read-write.js');
  1. 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();
});

JSON File Syntax Example - (I have used the objectarray in the json-read-write.js)

 {
  "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.

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published