To append data to a JSON file in JavaScript, you can use the fs
module that comes with Node.js. Here's an example code snippet that demonstrates how to do this:
index.tsx509 chars26 lines
In this example, we first read the JSON data from the file using readFile()
function. We then parse the data into a JavaScript object using JSON.parse()
.
After that, we append the new data to the object using push()
function.
Finally, we overwrite the original file with the updated JSON data using writeFile()
function. We pass the JSON.stringify()
function to writeFile()
to convert the object back to a JSON string before writing to the file.
Note: This code assumes that data.json
file already exists and has valid JSON data inside it.
gistlibby LogSnag