Wednesday, 31 May 2017

Alternative ways to parse local JSON file with functional approach?

Below is the way I usually use to safely parse local JSON data in Node environment, mostly config files and some other relevant data:

const fs = require('fs')

let localDb
let parsedData

try {
    localDb = fs.readFileSync('./file.json', 'utf8')
    parsedData = JSON.parse(localDb)
} catch (err) {
    throw err
}

exports.data = parsedData

In the end, I export the parsed data from the JavaScript file for usage. While this works perfectly fine, I'm curious to know if there are better ways to do the same thing with a functional approach.



via cinnaroll45

No comments:

Post a Comment