將JSON轉換成CSV文件:json2csv

jopen 10年前發布 | 93K 次閱讀 json2csv JSON開發包

json2csv能夠將JSON格式的數據轉換成帶標題的CSV格式文件。

特性:

  • Uses proper line endings on various operating systems
  • Handles double quotes
  • Allows custom column selection
  • Reads column selection from file
  • Pretty writing to stdout
  • Supports optional custom delimiters
  • Not create CSV column title by passing hasCSVColumnTitle: false, into params.
  • If field is not exist in object then the field value in CSV will be empty.

示例代碼:

var json2csv = require('json2csv');

var json = [
  {
    "car": "Audi",
    "price": 40000,
    "color": "blue"
  }, {
    "car": "BMW",
    "price": 35000,
    "color": "black"
  }, {
    "car": "Porsche",
    "price": 60000,
    "color": "green"
  }
];

json2csv({data: json, fields: ['car', 'price', 'color']}, function(err, csv) {
  if (err) console.log(err);
  fs.writeFile('file.csv', csv, function(err) {
    if (err) throw err;
    console.log('file saved');
  });
});

將JSON轉換成CSV文件:json2csv

項目主頁:http://www.baiduhome.net/lib/view/home/1388545050156

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!