Skip to content

Instantly share code, notes, and snippets.

@ff6347
Forked from iwek/csv-to-json.js
Created April 29, 2014 15:26
Show Gist options
  • Select an option

  • Save ff6347/11403636 to your computer and use it in GitHub Desktop.

Select an option

Save ff6347/11403636 to your computer and use it in GitHub Desktop.
//var csv is the CSV file with headers
function csvJSON(csv){
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);
}
//return result; //JavaScript object
return JSON.stringify(result); //JSON
}
Copy link

ghost commented Oct 27, 2016

how to add csv file here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment