node.JS

Random NodeJS Notes, JSON handling

To read existing JSON content from local files:

1
2
3
4
5
6
7
8
const fs = require('fs')
try {
const dataBuffer = fs.readFileSync('<json file name>')
const dataJSON = dataBuffer.toString()
return JSON.parse(dataJSON)
} catch (e) {
return []
}

To save text to JSON files:

1
2
3
const fs = require('fs')
const dataJSON = JSON.stringify(notes)
fs.writeFileSync('<json file name>', dataJSON)