Node.js Project to Convert Raw JSON File to CSV Using json2csv Library in Javascript Full Tutorial For Beginners

Node.js Project to Convert Raw JSON File to CSV Using json2csv Library in Javascript Full Tutorial For Beginners

 

Welcome folks today in this blog post we will be converting json file to csv using json2csv library in node.js and javascript. All the full source code of the application is given below.

 

 

 

Get Started

 

 

 

In order to get started we need to install json2csv library by issuing the npm command

 

npm i json2csv

 

After installing this library inside your node.js project. Now we will be converting data.json file to csv file. For this we need to create data.json file and copy paste the following code

 

data.json

 

 

[
    {
        "name":"Gautam",
        "age":"24",
        "subject":"computer science"
    },
    {
        "name":"Rahul",
        "age":"23",
        "subject":"Maths"
    },
    {
        "name":"Soham",
        "age":"43",
        "subject":"Hindi"
    },
    {
        "name":"Anjali",
        "age":"34",
        "subject":"Science"
    }
]

 

 

 

Now we can convert this json file to a pretty printed table using json2csv library. For this you need to install this library globally inside your system by using the -g flag

 

npm i -g json2csv

 

After this you can issue the following command to convert json to pretty table

 

 

json2csv -i data.json -f name,age,subject -p

 

 

 

See also  Node.js Project to Check HTTP Status Code (Active or Dead),Header of Website Link or URL Using link-check Library in Javascript Full Project For Beginners

 

 

Here we are taking the input file by using the -i flag and then we are providing the fields such as name,age and subject by using the -f flag and then we are using the flag -p to print the data inside a pretty table.

 

 

Converting JSON to CSV File

 

 

 

Now we can convert the json file to csv file by using this command

 

json2cav -i data.json -f name,age,subject -o csvfile.csv

 

Here we are using a additional flag -o for output file we are providing csvfile.csv

 

 

 

 

 

 

 

Programmatic Usage in Node.js

 

 

 

Now to use it inside node.js you need to install json2csv library by using the npm command

 

npm i json2csv

 

 

After this you can make an index.js file and copy paste the following code

 

 

index.js

 

 

const { Parser } = require('json2csv');

const fields = ["name","age","subject"];
const opts = { fields };

const myData = [{
  "name":"gautam",
  "age":"12",
  "subject":"computer science"
},
{
  "name":"gautam",
  "age":"12",
  "subject":"computer science"
},
{
  "name":"gautam",
  "age":"12",
  "subject":"computer science"
},
{
  "name":"gautam",
  "age":"12",
  "subject":"computer science"
}]

try {
  const parser = new Parser(opts);
  const csv = parser.parse(myData);
  console.log(csv);
} catch (err) {
  console.error(err);
}

 

See also  How to Show and Hide Div on Mouse Click in jQuery Using Javascript Full Project For Beginners

 

If you execute this node.js application by executing the commands as follows

 

node index.js

 

 

Leave a Reply