Last active
July 9, 2018 14:50
-
-
Save SamDecrock/d37efd38b0d5c31b3aa5f18780fda7d3 to your computer and use it in GitHub Desktop.
Conversts json (or any other seperated data) to an array of json objects (provided that there's a row with header information)
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
function csv2jsonObjects (csv) { | |
var lines = csv.split('\n'); | |
var keys = []; | |
var json = []; | |
for (var i = 0; i < lines.length; i++) { | |
var line = lines[i]; | |
var parts = line.split(';'); | |
if(i == 0){ | |
// keys are on the first line: | |
keys = parts; | |
}else{ | |
var entry = {}; | |
for (var j = 0; j < parts.length; j++) { | |
var part = parts[j]; | |
var key = keys[j]; | |
entry[key] = part; | |
}; | |
json.push(entry); | |
} | |
}; | |
return json; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment