Last active
October 5, 2018 08:26
-
-
Save adalenv/da156abdf1e3ffb195fb7ba6f9295ab9 to your computer and use it in GitHub Desktop.
Parse csv file to json object
This file contains hidden or 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
//-read file-\\ | |
var openFile = function(file) { | |
var reader = new FileReader(); | |
reader.onload = function(){ | |
var f= csvToJson(reader.result); | |
echo(f); | |
}; | |
reader.readAsText(file.target.files[0]); | |
}; | |
//-convert from tab delimited to json array on js-\\ | |
function csvToJson(data){ | |
var r = []; | |
var t = data; | |
var v = t.split("\r\n"); | |
for (var i = 0; i < v.length; i++) { | |
var w = v[i].split("\t"); | |
g=[]; | |
for(j=0; j<w.length; j++){ | |
g.push(w[j]); | |
} | |
r.push(g); | |
} | |
return r; | |
} | |
function echo(f){ | |
console.log(f); | |
document.querySelector('#echoDiv').innerHTML=JSON.stringify(f, null, 2); | |
} | |
//-select file-\\ | |
document.querySelector('html').innerHTML="<input type='file' onchange='openFile(event);document.querySelector(\"#refresh\").style.display=\"block\";'><br>"; | |
document.querySelector('html').innerHTML+="<input type='button'style='display:none' id='refresh' value='Refresh' onclick='echo()'><br><pre><code id='echoDiv'></code></pre>"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment