如何在NodeJS和ExpressJS中将嵌套JSON对象数组转换为CSV

时间:2017-02-16 13:31:24

标签: json node.js mongodb csv express

我想将CSV文件从Server发送到Client。 我有使用ExpressJS Response从MongoDB获得的JSON对象。

这是我的NodeJS代码,它接受来自客户的发布请求,其中包含正文中的Id和一个集合名称参数,并且在响应中,它获取与这些ID相关的数据。

app.post('/api/v1/imagelist_data/raw-data/:db', function(req, res) {
    console.log("I received a NEW POST raw-data request");
    var dbname = req.params.db;

    var myarray = req.body._id;

    //  Setting Collection Name
    var mycollection = db.collection(dbname);

    // Convert the list of ids to mongo object ids
    var objectIds = myarray.map(function(item) {
        return mongojs.ObjectId(item);
    });

    console.log(objectIds);

    mycollection.find({
        _id: {
            $in: objectIds
        }
    }, function(err, docs) {

        res.json(docs);


    });

现在我在客户端获取响应中的对象数组。 我想用这些对象数组制作一个CSV文件。

对象数组看起来像这样。

[
{
    "_id": "58405524d70210dc299ca275",
    "pictureNumber": 1,
    "scAttitude": [
        0,
        0,
        0,
        1
    ],
    "scPosition": [
        1,
        0,
        0
    ],
    "integrationTime": 10,
    "time": "2016-12-01T16:51:57.000Z",
    "offset": 1,
    "gain": 2,
    "ledStatus": false,
    "imageType": "Image",
    "cameraType": "Mongo",
    "testPatternStatus": false,
    "temperatureCCD": 189,
    "temperatureLED1": 162,
    "temperatureLED2": 152,
    "temperatureVBG": 490,
    "temperatureGND": 0,
    "imageValid": true,
    "remarks": " Reception time: Thu Dec 01 17:51:47 CET 2016",
    "imageSize": 2271320
},
{
    "_id": "586bc534b31a89bb45b0083e",
    "pictureNumber": 2,
    "scAttitude": [
        1,
        0,
        0,
        1
    ],
    "scPosition": [
        1,
        0,
        0
    ],
    "integrationTime": 20,
    "time": "2016-12-02T16:51:57.000Z",
    "offset": 2,
    "gain": 0,
    "ledStatus": false,
    "imageType": "Image",
    "cameraType": "MOngo",
    "testPatternStatus": false,
    "temperatureCCD": 189,
    "temperatureLED1": 162,
    "temperatureLED2": 152,
    "temperatureVBG": 490,
    "temperatureGND": 0,
    "imageValid": true,
    "remarks": " Reception time: Thu Dec 01 17:51:47 CET 2016",
    "imageSize": 2271320
}]

我应该在服务器端还是客户端执行此处理?

我想通过点击按钮下载客户端数据的CSV文件。

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:0)

您可以使用以下任一模块:

(如果它是CSV或TSV,请不要担心 - 只需确保使用正确的分隔符,在您的情况下使用逗号)或其他内容:

与手动编写正确的输出相比,这更容易且更不容易出错,尤其是当您有任何特殊字符时。

答案 1 :(得分:0)

我的建议是在服务器端生成CSV文件并将其作为附件返回给客户端。此方法可确保CSV文件在所有设备(笔记本电脑/电话/桌子等)上都相同。此外,您可以避免在每个不同的客户端上重新实现CSV。

使用expressjs中的csv文件进行响应:

mycollection.find({
    _id: {
        $in: objectIds
    }
}, function(err, docs) {
    if (err) {
      res.status(400);
      res.send('Unexpected db error');
    }

    var csv = convertCollectionToCsv(docs);
    if (!csv) {
      res.status(400);
      res.send('Invalid csv format');
    }

    res.attachment('filename.csv');
    res.status(200).send(csv);
});

您可以为CSV转换器/解析器选择第三方库,或者,如果它太简单,您可以创建自己的库。 以下是创建csv文件的典型方法:



var data = getData();
var csv = convertCollectionToCsv(data);
console.log(csv);

function convertCollectionToCsv(docs, seperator) {
  if (!(Array.isArray(docs) && docs.length > 0)) {
    return;
  }

  seperator = seperator || ',';

  var header = createHeader(docs);
  var body = docs.map(convertObjectsToCsv);
  var csv = []
    .concat([header])
    .concat(body)
    .join('\n');

  return csv;

  function createHeader(docs) {
    var headRow = docs[0];
    var header = Object.keys(headRow);
    return header.join(seperator);
  }

  function convertObjectsToCsv (doc) {
    // special treatment for dates, nested arrays or objects
    // for instance:
    doc['scAttitude'] = (doc['scAttitude'] || []).join('-');
    doc['scPosition'] = (doc['scPosition'] || []).join('-');

    var values = getValuesFromObject(doc);
    return values.join(seperator);
  }
}

function getValuesFromObject(obj) {
  if (typeof obj !== 'object' || obj === null) {
    return [];
  }

  var keys = Object.keys(obj);
  var values = [];
  for (var i = 0; i < keys.length; ++i) {
    values.push(obj[keys[i]]);
  }

  return values;
}

function getData() {
  return [{
      "_id": "58405524d70210dc299ca275",
      "pictureNumber": 1,
      "scAttitude": [
          0,
          0,
          0,
          1
      ],
      "scPosition": [
          1,
          0,
          0
      ],
      "integrationTime": 10,
      "time": "2016-12-01T16:51:57.000Z",
      "offset": 1,
      "gain": 2,
      "ledStatus": false,
      "imageType": "Image",
      "cameraType": "Mongo",
      "testPatternStatus": false,
      "temperatureCCD": 189,
      "temperatureLED1": 162,
      "temperatureLED2": 152,
      "temperatureVBG": 490,
      "temperatureGND": 0,
      "imageValid": true,
      "remarks": " Reception time: Thu Dec 01 17:51:47 CET 2016",
      "imageSize": 2271320
  },
  {
      "_id": "586bc534b31a89bb45b0083e",
      "pictureNumber": 2,
      "scAttitude": [
          1,
          0,
          0,
          1
      ],
      "scPosition": [
          1,
          0,
          0
      ],
      "integrationTime": 20,
      "time": "2016-12-02T16:51:57.000Z",
      "offset": 2,
      "gain": 0,
      "ledStatus": false,
      "imageType": "Image",
      "cameraType": "MOngo",
      "testPatternStatus": false,
      "temperatureCCD": 189,
      "temperatureLED1": 162,
      "temperatureLED2": 152,
      "temperatureVBG": 490,
      "temperatureGND": 0,
      "imageValid": true,
      "remarks": " Reception time: Thu Dec 01 17:51:47 CET 2016",
      "imageSize": 2271320
  }];
}
&#13;
&#13;
&#13;