2

In my CSV file, I have this:

id | level | date
1  |   2   | 01-01-2017
2  |   4   | 01-01-2017

After using the code from here, I am able to get this string on my console:

id,level,date
1,2,"01-01-2017"
2,4,"01-01-2017"

The code I used for the results above to be converted to JSON is from here:

  var lines=csv.split("\n");
  var result = [];
  var headers=lines[0].split(",");

    for(var i=1;i<lines.length;i++){
      var obj = {};
      var currentline=lines[i].split(",");

        for(var j=0;j<headers.length;j++){
          obj[headers[j]] = currentline[j];
        }

      result.push(obj);
    }

However, the code results in this:

(2) [{…}, {…}]
0:{id: "1", level: "2", date: ""01-01-2017""}
1:{id: "2", level: "4", date: ""01-01-2017""}

How do I remove the additional double quotes? My desired results should be as such:

(2) [{…}, {…}]
0:{id: 1, level: 2, date: "01-01-2017"}
1:{id: 2, level: 4, date: "01-01-2017"}
icedmilocode
  • 95
  • 1
  • 2
  • 15

1 Answers1

0

In the end, I did it this way:

for (var m=0; m<array.length; m++){
  array[m].id    = array[m].id.substring(1, array[m].id.length-1);
  array[m].level = array[m].level.substring(1, array[m].level.length-1);
  array[m].date  = array[m].date.substring(1, array[m].date.length-1);
}

May not be the best way but it works for me in the current context. Am still looking for ways to improve on this. Thank you!

icedmilocode
  • 95
  • 1
  • 2
  • 15