fledgling
fledgling

Reputation: 1051

Read value from json file using node.js

I am new to node.js and kind of stuck here. I have a json file keyValue.json which resembles this

[ {
    "key": "key1",
    "value": "value1"
  },   
  {
    "key": "key2",
    "value": "value2"
  } 
]

For a particular key I need to get the value.

function filterValue() {
  const category = {};
  category.filter = "key1" //this is not static value. This changes dynamically
  category.value = //Need to read the keyValue.json file and pick the value for key1 - which is value1
  return category;
}

How can this be achieved in node.js. Thanks for helping out.

Upvotes: 4

Views: 10376

Answers (2)

Dinusha Naveen
Dinusha Naveen

Reputation: 170

If you are having json object named keyValue. first import it and named it as keyValue, then try this,

function filterValue(req, res, next) {
  keyValue
     .find({ key: "key1" })
     .exec()
     .then(result => {
        return result.value
     })  
}

Upvotes: -1

Victoria French
Victoria French

Reputation: 766

I am assuming that you have your json parsed into a variable. So my example will use this:

const data = [ 
  {
    key: "key1",
    value: "value1"
  },   
  {
    key: "key2",
    value: "value2"
  } 
];

If not, then in node we can just require it as long as it is a static file. But this will be loaded once for the execution span of the app.

const data = require('./file.json');

If it needs to be dynamically read because it is changing for some reason then you need to use fs (always do it async in node).

const fs = require('fs');

fs.readFile('./file.json', 'utf8', function(err, contents) {
    if (err) {
      // we have a problem because the Error object was returned
    } else {
      const data = JSON.parse(contents);
      ... use the data object here ...
    }
});

If you prefer Promises to callbacks:

const fs = require('fs');

// ES7 Version
const getJsonFile = (filePath, encoding = 'utf8') => (
   new Promise((resolve, reject) => {
      fs.readFile(filePath, encoding, (err, contents) => {
          if(err) {
             return reject(err);
          }
          resolve(contents);
      });
   })
     .then(JSON.parse)
);

// ES6 Version
const getJsonFile = function getJsonFile(filePath, encoding = 'utf8') {
   return new Promise(function getJsonFileImpl(resolve, reject) {
      fs.readFile(filePath, encoding, function readFileCallback(err, contents) {
          if(err) {
             return reject(err);
          }
          resolve(contents);
      });
   })
     .then(JSON.parse);
};

Now with either of these you can simply use the Promise

getJsonFile('./file.json').then(function (data) { ... do work });

or if using async

const data = await getJsonFile('./file.json');

So now we would want a composable filter to use with Array's find method. In the latest versions of Node (Carbon and above):

const keyFilter = (key) => (item) => (item.key === key);

If you are node 6 or below lets do this the old way

const keyFilter = function keyFilter(key) {
   return function keyFilterMethod(item) {
      return item.key === key;
   }
};

Now we can use this filter on the array changing out the key we are looking for at any time (because it is composable).

const requestedItem = data.find(keyFilter('key2');

If the key doesn't exist undefined will be returned, otherwise, we have the object.

const requestedValue = requestedItem ? requestedItem.value : null;

This filter can also be used for other searches in the Array such as finding the index

const index = data.findIndex(keyFilter('key2'));

If "key" was allowed to be duplicated in the array, you could grab them all using filter

const items = data.filter(keyFilter('key2'));

To sum it all up we would probably put our areas of concern in different files. So maybe a getJsonFile.js and a keyFilter.js so now put together we would look something like this (calling this file readKeyFromFile):

const getJsonFile = require('./getJsonFile');
const keyFilter = require('./keyFilter');

module.exports = async (filePath, keyName) => {
   const json = await getJsonFile(filePath);
   const item = json.find(keyFilter(keyName));
   return item ? item.value : undefined;
};

and our consumer

const readKeyFromFile = require('./readKeyFromFile');

readKeyFromFile('./file.json', 'key2').then((value) => {
   console.log(value);
};

There are so many combinations of how you can do this once these fundamentals are understood. So have fun with them. :)

Upvotes: 10

Related Questions