When working with Node.js, the following steps can help you tackle your programming questions.
Firstly, you'll need to read and write a .json file using the code snippet below:
const fs = require("fs");
let usersjson = fs.readFileSync("users.json","utf-8");
Next, you'll want to transform a json string into a JavaScript array like so:
let users = JSON.parse(usersjson);
After that, you can append an object to an array using the following line of code:
users.push(obj);
To transform the updated array back into a json string, use this piece of code:
usersjson = JSON.stringify(users);
Finally, don't forget to save the modified json file by executing the following command:
fs.writeFileSync("users.json",usersjson,"utf-8");