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

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

 

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

 

 

 

Installation

 

 

 

You can install this library in two ways either globally as a command line library  or you can install as programatically for using it in node.js by installing it as a dependency inside node.js project

 

for cli

 

npm i -g jsonexport

 

After installing it globally you can use the library commands anywhere

 

to convert json to csv you will enter the following command

 

jsonexport data.json output.csv

 

 

 

 

 

Now you can also use it inside node.js app by installing it locally as a dependency

 

npm i jsonexport

 

After installing it you can make a index.js file and copy paste the following code

 

index.js

 

const jsonexport = require('jsonexport');

jsonexport({lang: 'Node.js', module: 'jsonexport'}, {rowDelimiter: '|'}, function(err, csv){
    if (err) return console.error(err);
    console.log(csv);
});

 

 

Here we are using the delimiter to convert json to csv. If you execute this node.js app you will see the following result which is shown below.

See also  How to Build a Cron Job Task Scheduler Using Node-Cron in Node.js Full Tutorial

 

 

 

 

Stream

const jsonexport = require('jsonexport');
const fs = require('fs');

const reader = fs.createReadStream('data.json');
const writer = fs.createWriteStream('out.csv');

reader.pipe(jsonexport()).pipe(writer);

 

 

 

Promise

const jsonexport = require('jsonexport')
try {
    const csv = await jsonexport({lang: 'Node.js', module: 'jsonexport'}, {rowDelimiter: '|'});
} catch (err) {
    console.error(err);
}

 

 

JSON Array Example

 

 

const jsonexport = require('jsonexport');

const contacts = [{
    name: 'Bob',
    lastname: 'Smith'
},{
    name: 'James',
    lastname: 'David'
},{
    name: 'Robert',
    lastname: 'Miller'
},{
    name: 'David',
    lastname: 'Martin'
}];

jsonexport(contacts, function(err, csv){
    if (err) return console.error(err);
    console.log(csv);
});

 

 

 

Complex Array

 

 

const jsonexport = require('jsonexport');

const contacts = [{
   name: 'Bob',
   lastname: 'Smith',
   family: {
       name: 'Peter',
       type: 'Father'
   }
},{
   name: 'James',
   lastname: 'David',
   family:{
       name: 'Julie',
       type: 'Mother'
   }
},{
   name: 'Robert',
   lastname: 'Miller',
   family: null,
   location: [1231,3214,4214]
},{
   name: 'David',
   lastname: 'Martin',
   nickname: 'dmartin'
}];

jsonexport(contacts, function(err, csv){
    if (err) return console.error(err);
    console.log(csv);
});

 

 

 

JSON Object Example

 

 

 

const jsonexport = require('jsonexport');

const stats = {
    cars: 12,
    roads: 5,
    traffic: 'slow'
};

jsonexport(stats, function(err, csv){
    if(err) return console.error(err);
    console.log(csv);
});

 

 

Complex Object

 

 

const jsonexport = require('jsonexport');

const stats = {
    cars: 12,
    roads: 5,
    traffic: 'slow',
    speed: {
        max: 123,
        avg: 20,
        min: 5
    },
    size: [10,20]
};

jsonexport(stats, function(err, csv){
    if(err) return console.error(err);
    console.log(csv);
});

 

Leave a Reply