Friday 12 June 2015

Node.js - Reading data from JSON file or JSON object.

Prerequisites:
Node.js and NPM is already installed, click below link for installing Node.js and NPM.
http://automation-home.blogspot.in/2015/05/installing-nodejs-and-npm-on-windows.html

Below steps demonstrates reading the data from JSON file.
File Name: BooksInfo.json
[
  {
    "book_id" : "1125",
    "book_name" : "Head First Java",
  "book_cost" : "620"
  },
  {
    "book_id" : "8876",
    "book_name" : "Node.js in Action",
  "book_cost" : "2097"
  },
  {
    "book_id" : "5484",
    "book_name" : "Angular JS Testing Cookbook",
  "book_cost" : "1519"
    }
]

File Name: PrintJsonData.js
var fs = require('fs');
fs.readFile('BookInfo.json', 'utf8', function (err, data) {
  if (err) throw err;
  var jsonData = JSON.parse(data);
  console.log("--------------Books Information --------");
  for (var i = 0; i < jsonData.length; ++i) {
    console.log("Book Id"+jsonData[i].book_id);
    console.log("Book Name Id"+jsonData[i].book_name);
    console.log("Book Cost Id"+jsonData[i].book_cost);
  console.log("----------------------------------------");
  }
});


Open the command prompt.

Open the folder path in command prompt where the 'BooksInfo.json' and 'PrintJsonData.js' are placed.

Type the command 'node PrintJsonData.js' and press enter key.



Below steps demonstrates reading data from JSON object.
File Name: PrintJsonData.js
var response = '[{"Mobile":iOS , "Cost":55000}]';
var jsonObject = JSON.parse(response);
console.log(jsonObject[0].Mobile);
console.log(jsonObject[0].Cost);

Type the command 'node PrintJsonData.js' and press enter key.

--------------------------------------------------------------------------------------------------------------------------

Below steps demonstrates reading the data from JSON file using 'jsonfile' package.

Import the 'jsonfile' package using the npm command 'npm install jsonfile --save'.
Note: if 'jsonfile' package is already installed, you can ignore this step(i.e. installing 'jsonfile' package).


File Name: BooksInfo.json
[
  {
    "book_id" : "1125",
    "book_name" : "Head First Java",
  "book_cost" : "620"
  },
  {
    "book_id" : "8876",
    "book_name" : "Node.js in Action",
  "book_cost" : "2097"
  },
  {
    "book_id" : "5484",
    "book_name" : "Angular JS Testing Cookbook",
  "book_cost" : "1519"
    }
]

File Name: PrintJsonData1.js
var jf = require('jsonfile')
var file = 'BookInfo.json'
jf.readFile(file, function(err, jsonData) {
  if (err) throw err;
  console.log("--------------Books Information --------");
  for (var i = 0; i < jsonData.length; ++i) {
    console.log("Book Id : "+jsonData[i].book_id);
    console.log("Book Name : "+jsonData[i].book_name);
    console.log("Book Cost : "+jsonData[i].book_cost);
    console.log("----------------------------------------");
  }

});

Type the command 'node PrintJsonData1.js' and press enter key.

--------------------------------------------------------------------------------------------------------------------------

Below steps demonstrates reading data from JSON file using 'jsonfile' package - 'readFileSync' function.

Import the 'jsonfile' package using the npm command 'npm install jsonfile --save'.
Note: if 'jsonfile' package is already installed, you can ignore this step(i.e. installing 'jsonfile' package).


File Name: BooksInfo.json
[
  {
    "book_id" : "1125",
    "book_name" : "Head First Java",
  "book_cost" : "620"
  },
  {
    "book_id" : "8876",
    "book_name" : "Node.js in Action",
  "book_cost" : "2097"
  },
  {
    "book_id" : "5484",
    "book_name" : "Angular JS Testing Cookbook",
  "book_cost" : "1519"
    }
]

File Name: PrintJsonDataSync.js
var jf = require('jsonfile')
var file = 'BookInfo.json'

var jsonData = jf.readFileSync(file);
for (var i = 0; i < jsonData.length; ++i) {
    console.log("Book Id : "+jsonData[i].book_id);
    console.log("Book Name : "+jsonData[i].book_name);
    console.log("Book Cost : "+jsonData[i].book_cost);
    console.log("----------------------------------------");
}

Type the command 'node PrintJsonDataSync.js' and press enter key.

2 comments: