Skip to content

Instantly share code, notes, and snippets.

@SamDecrock
Last active July 9, 2018 14:50
Show Gist options
  • Save SamDecrock/d37efd38b0d5c31b3aa5f18780fda7d3 to your computer and use it in GitHub Desktop.
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)
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