0

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 :

enter image description here 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 : enter image description here

As you can see , it's completely wrong : here is for example the first outputed element : enter image description here 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 ??

firasKoubaa
  • 6,439
  • 25
  • 79
  • 148
  • 1
    Can you describe exactly what is wrong with it? Is it just that hte labels are wrong? – Lucas Hendren Sep 10 '17 at 03:09
  • you need to install the package csvtojson with simple npm install --save csvtojson@latest https://stackoverflow.com/questions/16831250/how-to-convert-csv-to-json-in-node-js –  Sep 10 '17 at 03:10
  • @Legman : i ve added a snapshot of an example of wrong converted data – firasKoubaa Sep 10 '17 at 03:14
  • @headmax same problem – firasKoubaa Sep 10 '17 at 03:15
  • Try to convert online your CSV to show if the result and put to a validator json http://www.csvjson.com/csv2json https://jsonformatter.curiousconcept.com/ –  Sep 10 '17 at 03:20

1 Answers1

0

From the csvtojson API docs:

noheader:Indicating csv data has no header row and first row is data row. Default is false.

So maybe in your Converter construct add noheader:true.

Rnice4christ
  • 78
  • 2
  • 4