0

I am making a client side program to convert a CSV to JSON. I have tried following along to this solution here but I can't quite get what I want. I want to save the JSON to a variable to that I can add headers and do other manipulations to the JSON file.

import csv from 'csv';

handleFiles = () => {
    console.log(this.refs.file.files[0])
    var reader = new FileReader();
    reader.onload = ()=> {
        let jsonfile = reader.readAsBinaryString(this.refs.file.files[0]);
        }

}

when I console log jsonfile it is undefined. I am trying to get my data into an array of objects or an array of arrays.

tdammon
  • 599
  • 1
  • 7
  • 26

1 Answers1

0

I tried and succeeded with this code with es6. Good lucky

  var csv = require("node-csv").createParser();
  csv.parseFile('./test/ahihi.csv', (err, data) => {
  var result = {};
  var keys = data[0].slice(1);
  var data = data.slice(1);
  var obj;
  for(var i=0; i<keys.length; i++) {
    obj = {};
    data.forEach((dataset) => {
      obj[dataset[0]] = dataset[i+1];
    })
    result[keys[i]] = obj;
  }
  console.log(result);
})
it dũng
  • 29
  • 2