Last active
May 4, 2022 08:38
-
-
Save krrishd/5b8d331adf58b9be13fb to your computer and use it in GitHub Desktop.
converting csv 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 csv2json = (function() { | |
function convert(data) { | |
var json = []; | |
var d = data.split('\n'); | |
var keys = d[0].split(','); | |
d = d.splice(1); | |
d.forEach(function(data) { | |
var keyVal = data.split(','); | |
var obj = {}; | |
keys.forEach(function(keyData, n) { | |
obj[keyData] = keyVal[n]; | |
}); | |
json.push(obj); | |
}); | |
return json; | |
} | |
return function(data) { | |
return convert(data); | |
} | |
})(); | |
window.csv2json = csv2json; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment