Skip to content

Instantly share code, notes, and snippets.

@kublaj
Forked from iwek/tsv-to-json.js
Created February 6, 2014 11:33
Show Gist options
  • Save kublaj/8842496 to your computer and use it in GitHub Desktop.
Save kublaj/8842496 to your computer and use it in GitHub Desktop.
//var tsv is the TSV file with headers
function tsvJSON(tsv){
var lines=tsv.split("\n");
var result = [];
var headers=lines[0].split("\t");
for(var i=1;i<lines.length;i++){
var obj = {};
var currentline=lines[i].split("\t");
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
}
@zacherygentry
Copy link

On line 4, I am using tsv.trim().split("\n"); to ensure there is not an empty object at the end.

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