Saturday, February 11, 2023

Append a new record to JSON file

 I have a bookmarks.json that contains records with attributes: id, title, link. Write a node js script to append a new record to this existing file.


const fs = require("fs");


// Define the new record

const newRecord = {

  id: 4,

  title: "Google",

  link: "https://www.google.com",

};


// Read the bookmarks.json file

fs.readFile("bookmarks.json", "utf8", (err, data) => {

  if (err) throw err;


  // Parse the JSON data

  const bookmarks = JSON.parse(data);


  // Append the new record

  bookmarks.push(newRecord);


  // Write the updated bookmarks to the file

  fs.writeFile("bookmarks.json", JSON.stringify(bookmarks), "utf8", (err) => {

    if (err) throw err;

    console.log("The new record has been successfully added to bookmarks.json");

  });

});


In this script, the fs module is used to read and write the bookmarks.json file. The new record is defined as a JavaScript object, and then appended to the existing bookmarks using the push method. The updated bookmarks are then written back to the file using the writeFile method. The script logs a message to the console when the write operation is complete.


What is fs module?


The fs module is a built-in module in Node.js that provides a set of APIs for interacting with the file system. It allows you to perform various file-related operations such as reading and writing files, creating and deleting directories, checking file and directory existence, etc.


The fs module is commonly used for reading and writing files in Node.js, especially for storing and retrieving data in a persistent manner. For example, you can use the fs module to read a JSON file, modify the data, and write the updated data back to the file.


Here's a simple example that demonstrates the use of the fs module to read a file and print its contents to the console:


const fs = require("fs");


fs.readFile("file.txt", "utf8", (err, data) => {

  if (err) throw err;

  console.log(data);

});


In this example, the fs.readFile method is used to read the contents of a file named file.txt. The method takes three arguments: the name of the file, the encoding of the file, and a callback function that is called when the read operation is complete. The contents of the file are passed as a string to the callback function.


How to run the nodejs script in a terminal?


To run a Node.js script in a terminal, you need to have Node.js installed on your system. Once you have Node.js installed, you can run a script by using the following command in the terminal:


node <script-name>.js


For example, if you have a script named app.js, you can run it using the following command:


node app.js


This will execute the script and display any output generated by the script in the terminal. If there are no errors in the script, it will run to completion and exit. If there are errors, the error message will be displayed in the terminal and the script will exit immediately.