如何将CSV中的http.get响应转换为JSON NodeJS

时间:2020-05-09 21:40:46

标签: javascript httpresponse csvtojson

http.get请求返回csv.gz文件。 解压缩后,我想将其转换为JSON并上传到mongoDB 如何在流上实现转换功能?

我的代码如下:

  const download = (url, dest) => {
  const request = https.get(url, function(response) {
    const decompress = zlib.createGunzip();
    const file = fs.createWriteStream('./downloads/'+ dest);
    response.pipe(decompress).on('data', (chunk) => {
      console.log(chunk)
    })
    .pipe(file);
  });
  }

CSV至JSON将使用以下格式进行转换:

  const csvToJson = (csv) => {
    const [firstLine, ...lines] = csv.split('\n');
    return lines.map((line) => firstLine.split(',').reduce(
      (curr, next, index) => ({
        ...curr,
        [next]: line.split(',')[index],
      }),
      {},
    ));
  };

调用代码如下:

  linksToDownload.map((item) => {
    if (item.value !== undefined) {
    const URL = item.value;
    const DEST = item.name + ".json"
    download(URL, DEST);
    }
  })

此外,为什么nodemon重新启动几次? printscreen of console

预先感谢您的帮助

0 个答案:

没有答案