简体   繁体   English

使用NodeJS将CSV转换为JSON

[英]CSV to JSON using NodeJS

I'd like to convert a CSV file to a JSON object using NodeJS. 我想使用NodeJS将CSV文件转换为JSON对象。 The problem is that my CSV file is hosted on a special URL. 问题是我的CSV文件托管在特殊的URL上。

URL : My CSV here 网址: 我的CSV在这里

var fs = require("fs");
var Converter = require("csvtojson").Converter;
var fileStream = fs.createReadStream("myurl");
var converter = new Converter({constructResult:true});
converter.on("end_parsed", function (jsonObj) {
   console.log(jsonObj);
});
fileStream.pipe(converter);

Issue : 问题 :

Error: ENOENT, open 'C:\xampp\htdocs\GestionDettes\http:\www.banque-france.fr\fileadmin\user_upload\banque_de_france\Economie_et_Statistiques\Changes_et_Taux\page3_quot.csv'
at Error (native)

Edit #1 : 编辑#1:

Request.get(myurl, function (error, Response, body) {
  var converter = new Converter({
    constructResult: true,
    delimiter: ';'
  });
  converter.fromString(body,function(err, taux){
    console.log(taux); // it works
  });
});

I did just that in a module reading and writing on different protocol in different data formats. 我只是在一个模块中以不同的数据格式读写不同的协议来做到这一点。 I used request to get http resources. 我使用request来获取http资源。

If you want take a look at alinex-datastore . 如果您想看看alinex-datastore With this module it should work like: 有了这个模块,它应该像:

const DataStore = require('@alinex/datastore').default;

async function transform() {
    const ds = new DataStore('http://any-server.org/my.csv');
    await ds.load();
    await ds.save('file:/etc/my-app.json');
}

That should do it. 那应该做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM