Monday, May 7, 2018

How to write to a json file

1- Call the filesystem
var fs = require('fs')
2- Define the object to be added to the JSON file
var data = {
  name: "rafael"
}
3- Use filesystem's writeFile() function to write into a custom "data.json" file
fs.writeFile("./data.json", JSON.stringify(data), (err) => {
  console.log("write successful", err)
})
Where: JSON.stringify function saves the data as a string

That's it!

No comments:

Post a Comment