-3

I have a JSON data which want to download it as csv file. How to achieve this in node js? Does NodeJs has some predefined modules for this?

P.S. I also want to add some formatting such as adding Headers like MONTHLY REPORT etc. adding row colors etc.

6

3 Answers 3

1

Node.js json2csv module would work

Here are some code examples:

Install it through npm

npm install --save json2csv

Use it in your node.js app:

const json2csv = require('json2csv').parse;
const fields = ['field1', 'field2', 'field3'];
const opts = { fields };

try {
  const csv = json2csv(myData, opts);
  console.log(csv);
} catch (err) {
  console.error(err);
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you mean, how do you convert JSON to CSV, in Node JS:

Make sure to include a header, such as this, in csv:

      Usernames
Name1 | Name2 | Name3

1 Comment

This doesn't answer the question.
-2

I don't know about you guys, but i like small packages that just work as expected without a lot of extra configuration, try using jsonexport, works really well with objects, arrays, .. and its fast!

Install

npm i --save jsonexport

Usage

const jsonexport = require('jsonexport');

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

https://github.com/kauegimenes/jsonexport

1 Comment

Self promotion generally isn't a good look without explaining how they can use it. What is the csv argument? How can the original poster use it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.