-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.

Ashish Maity
  • 35
  • 1
  • 9
  • https://www.npmjs.com/package/json2csv – Muhammad Usman May 02 '18 at 12:14
  • See also these questions: [How to convert JSON array to CSV using Node.js?](https://stackoverflow.com/q/38244285/5764553), [node.js: Convert json array to csv](https://stackoverflow.com/q/27817645/5764553), and [Json to csv in node.js](https://stackoverflow.com/q/27481929/5764553). (Last one isn't as closely related.) – Andrew Myers May 02 '18 at 12:15
  • 1
    I'm voting to close this question as off-topic because the OP shows no effort / attempt – d4nyll May 02 '18 at 12:21
  • Some other questions: [Nested JSON to CSV format Node.js](https://stackoverflow.com/q/35245113/5764553) and [How to parse JSON object to CSV file using json2csv nodejs module](https://stackoverflow.com/q/20620771/5764553) – Andrew Myers May 02 '18 at 12:22
  • Possible duplicate of [How to convert JSON array to CSV using Node.js?](https://stackoverflow.com/questions/38244285/how-to-convert-json-array-to-csv-using-node-js) – Andrew Myers May 02 '18 at 12:42
  • 1
    By the way, I don't think that CSV can do row colors. It's pretty much a text-only format. The first line is usually where the headers are, but again, I don't think you can do any special formatting. – Andrew Myers May 02 '18 at 12:50

3 Answers3

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);
}
Adeojo Emmanuel IMM
  • 2,104
  • 1
  • 19
  • 28
0

In this how we should group some particular field under one headings in csv is like:

      Usernames
Name1 | Name2 | Name3
Partho63
  • 3,117
  • 2
  • 21
  • 39
-1

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

Kauê Gimenes
  • 1,278
  • 1
  • 13
  • 30