Ad

How To Parse Data From Db.json File Into The Server?

- 1 answer

First, I install faker js for using fake data into my projects. Then I module code Into the index.json file like this

let fs = require('fs')
let faker = require('faker')

After that, I received data from faker.js, here is it

let arr = []

for (let i = 0; i < 10; i++) {

    let obj = {}
    obj.name = faker.name.findName(),
    obj.email = faker.internet.email(),
    obj.phone = faker.phone.phoneNumber()

    arr.push(obj)

}

fs.writeFileSync('db.json', JSON.stringify(arr), function (err, data) {
    console.log('you recive data from faker js');
})

and it is created db.json file when I load this code

now I want to parse data into the server from db.json file.

How Can I do that?

Ad

Answer

Something like this will do the trick:

let parsed = JSON.parse(fs.readFileSync('db.json'));
Ad
source: stackoverflow.com
Ad