How to parse JSON files in Node.JS ?

Quick tip using fs: let data = fs.readFileSync(“myFile.json”, ‘utf8’); console.log(typeof JSON.parse(data)); There is another way to do this that requires less effort. Using require directly, like so: const data = require(“myFile.json”); console.log(typeof data); Hope it helps.

How to convert a String into JSON object in NodeJS

Just a few days ago I needed to do this and I found this useful function. Very simple: var jsonStr = “{“firstName”:”Siry”, “lastName”:”Mazari”}”; console.log(‘JSON String: %s’, jsonStr); var jsonObj = JSON.parse(jsonStr); console.log(‘First Name: %s’, jsonObj.firstName); console.log(‘Last Name: %s’, jsonObj.lastName); The variable jsonStr contains a String representation of a JSON object. The variable jsonObj contains the object that was parsed from …