I'm intending to load data from a CSV file and send it within Express Router as JSON data.
My CSV looks like this :
- There is no headers ,
- my delimiter of lines is simply the "/n" ,
and my values are under " "
"2171106","2017-07-03 00:27:24","France","3","Desktop","Mac","Firefox","2170248","77.98" "800309","2017-07-03 03:23:21","France","9","Desktop","GNU/Linux","Safari","2170296","85.39" "805789","2017-07-03 03:34:32","France","1","Tablet","iOS","Mobile Safari","2170299","61.49" "2171167","2017-07-03 04:42:47","France","1","Desktop","Windows","Chrome","2170314","39.99" "2171170","2017-07-03 04:50:02","France","1","Desktop","Windows","Chrome","2170320","20.89" "838601","2017-07-03 05:48:38","France","1","Smartphone","iOS","Mobile Safari","2170323","66.88" "2171173","2017-07-03 06:09:28","France","1","Desktop","Windows","Microsoft Edge","2170407","57.77" "1659566","2017-07-03 06:28:08","France","1","Smartphone","Android","Chrome Mobile","2170416","32.99" "835619","2017-07-03 06:46:00","France","4","Desktop","Windows","Firefox","2170419","203.93" "773872","2017-07-03 06:51:14","France","8","Tablet","iOS","Mobile Safari","2170422","285.79" "2171241","2017-07-03 06:54:30","France","1","Desktop","Mac","Safari","2170425","29.49" "2171238","2017-07-03 07:02:48","France","1","Desktop","Windows","Firefox","2170434","49.95" "1570588","2017-07-03 07:08:09","France","1","Desktop","Windows","Microsoft Edge","2170437","61.06" "2111374","2017-07-03 07:08:21","United Kingdom","1","Tablet","iOS","Mobile Safari","2170440","36.58" "1604941","2017-07-03 07:18:38","France","2","Tablet","iOS","Mobile Safari","2170452","30.89" "2095884","2017-07-03 07:25:34","France","1","Tablet","iOS","Mobile Safari","2170458","65.79" "2171259","2017-07-03 07:26:48","France","1","Desktop","Windows","Firefox","2170461","62.67" "860366","2017-07-03 07:30:46","France","1","Desktop","Windows","Chrome","2170464","137.67"
And to show it better , here is it :
i wanna just parse this data to an object , and send it within express as json .
i have used several libraries : csvjson , csvtojson , papaparse ... but something goes wrong with the conversion every time.
Here is my server code:
var express = require("express");
var app = express();
var path = require("path");
var fs = require('fs');
var Converter = require("csvtojson").Converter;
var fileStream = fs.createReadStream("data.csv");
var jsonData;
//new converter instance
var converter = new Converter({constructResult:true});
//end_parsed will be emitted once parsing finished
converter.on("end_parsed", function (jsonObj) {
jsonData = jsonObj
});
fileStream.pipe(converter);
app.get("/list",function(req,res){
res.json(jsonData);
});
app.listen(3000);
console.log("Running at Port 3000");
When running , the conversion goes wrong , it displays data like this :
As you can see , it's completely wrong :
here is for example the first outputed element :
Almost libraries are converting it in the same wrong way , i think that there is a missing config in my code . (tabulations sperataing lines maybe)
Any suggestions ??