Skip to content

Instantly share code, notes, and snippets.

@Shipow
Created March 16, 2015 14:06
Show Gist options
  • Save Shipow/d414ff89f5f9baeeea0d to your computer and use it in GitHub Desktop.
Save Shipow/d414ff89f5f9baeeea0d to your computer and use it in GitHub Desktop.
TSV to JSON
var tsvToJson = function(input){
var info = input.replace(/['"]/g,''),
lines = info.split('\n'),
firstLine = lines.shift().split('\t'),
json = [];
// Helper function to remove quotes
// and parse numeric values
var removeQuotes = function(string){
string = string.replace(/(['"])/g, "\\$1");
if (!isNaN(string)){
string = parseFloat(string);
}
return string;
};
$.each(lines, function(index, item){
var lineItem = item.split('\t'),
jsonLineEntry = {};
$.each(lineItem, function(index, item){
jsonLineEntry[firstLine[index]] = removeQuotes(item);
});
json.push(jsonLineEntry);
});
return json;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment