How to parse JSON object to CSV file using json2csv nodejs module

前端 未结 6 816
青春惊慌失措
青春惊慌失措 2020-12-10 07:44

I\'m currently learning how to parse a JSON object to a CSV file using the json2csv node module. Have never worked with JSON before, so this is all new to me.

My JSO

6条回答
  •  佛祖请我去吃肉
    2020-12-10 08:08

    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');
    const fs = require('fs');
    
    jsonexport({
     "car":[
      {
       "name":"Audi",
       "price":"40000",
       "color":"blue"
      }
     ]
    }, function(err, csv) {
      if (err) return console.error(err);
      fs.writeFile('cars.csv', csv, function(err) {
        if (err) return console.error(err);
        console.log('cars.csv saved');
      });
    });
    

    https://github.com/kauegimenes/jsonexport

提交回复
热议问题