Created
March 16, 2015 14:06
-
-
Save Shipow/d414ff89f5f9baeeea0d to your computer and use it in GitHub Desktop.
TSV to JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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