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. :)