All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Write Data Into CSV File In JavaScript

Last Updated : Mar 11, 2024

How To Write Data Into CSV File In JavaScript

In this article we will show you the solution of how to write data into csv file in JavaScript, in this we are going to use Node.js. the csvjson is a Node.js library to convert Simple CSV to JSON and JSON to CSV with stream support.

We have to create JSON file first to convert it to csv.

Step By Step Guide On How To Write Data Into CSV File In Javascript :-

At first, we have to create the JSON file first.

JSON file :
[
    {
        "name" : "peter" ,
        "city" : "tokyo" ,
        "gender" : "male",
        "email": "peter@gmail.com"
    },
    {
        "name" : "mary" ,
        "city" : "london" ,
        "gender" : "female",
        "email": "mary@gmail.com"
    },
    {
        "name" : "thomas" ,
        "city" : "paris" ,
        "gender" : "male",
        "email": "thomas@gmail.com"
    },
    {
        "name" : "john" ,
        "city" : "new york" ,
        "gender" : "male",
        "email": "john@gmail.com"
    }
]

Now install csvjson by npm install csvjson fs into the Terminal.

JavaScript file :

        <!DOCTYPE html>
<html lang = " en " >
<head>
    <meta charset = " UTF - 8" >
    <meta http-equiv = " X-UA-Compatible " content = " IE=edge " >
    <meta name = " viewport " content = " width = device-width , initial-scale = 1.0 " >
    <title> how to write data into csv file in javascript </title>
    <style>
        body {
            font-family : 'Lucida Sans', Verdana , sans-serif ;
        }
        h1 {
         font-size : larger ;
         font-weight : bolder ;
         color : rgb(113, 221, 113) ;
         text-align : center ;
        }
        h3 {
            text-align : center ;
        }
    </style>
</head>
<body>
    <h1> TALKERSCODE </h1>
    <h3> how to write data into csv file in javascript </h3>
    <script>
        var csvjson = require('csvjson')
var fs = require('fs')
fs.readFile('./dataJ.json','utf-8',(err, fileContent)={
  if(err){
    console.log(err)
    throw new Error(err)
  }
  //convert json to csv
  const csvData = csvjson.toCSV(fileContent,{
    headers : 'key'
  });
  fs.writeFile('./employeeData.csv',csvData,(err)={
    if(err){
      console.log(err)
      throw new Error(err)
    }
    console.log("Converted successfully")
  });
})
    </script>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as the instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As mentioned above, the <head> tag contains information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Now create a < style > tag to add style to the HTML page.
  6. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  7. <h1> tag used to add heading here.
  8. Now create a <script> tag to write the JavaScript file. And close it with </script> tag.
  9. First create a variable for csvjson to require the csvjson file and for fs to require the fs file
  10. .readFile() to read the json file
  11. Using toCSV () with the constant csvData.
  12. writeFile() to write the json to csv data.
  13. Console.log() to display the success message.

Conclusion :-

At last, here in conclusion, here we can say that with this article’s help, we know how to write data into CSV file using JavaScript.

I hope this article on how to write data into csv file in JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪