Created
September 7, 2022 12:37
-
-
Save JeremieLitzler/888f7dabfa0ff3b67e527910528357c4 to your computer and use it in GitHub Desktop.
Convert a 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
//Inspired from https://stackoverflow.com/a/59017439 | |
function csvToJSON(csv, seperator) { | |
var lines = csv.split("\n"); | |
var result = []; | |
var headers; | |
headers = lines[0].split(seperator); | |
for (var i = 1; i < lines.length; i++) { | |
var obj = {}; | |
if(lines[i] == undefined || lines[i].trim() == "") { | |
continue; | |
} | |
var words = lines[i].split(seperator); | |
for(var j = 0; j < words.length; j++) { | |
obj[headers[j].trim()] = words[j]; | |
} | |
result.push(obj); | |
} | |
console.log(result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment