Skip to content

Instantly share code, notes, and snippets.

@soscs
Forked from jonmaim/csv2js.js
Last active August 31, 2015 23:29
Show Gist options
  • Save soscs/c5819996df3235079025 to your computer and use it in GitHub Desktop.
Save soscs/c5819996df3235079025 to your computer and use it in GitHub Desktop.
Function takes a CSV (header + data) string as input and gives back a JS object.
// Start from https://gist.github.com/iwek/7154578#file-csv-to-json-js
// and fix the issue with double quoted values
function csvTojs(csv) {
var lines=csv.split("\n");
var result = [];
var headers = lines[0].split(",");
for(var i=1; i<lines.length; i++) {
var obj = {};
var row = lines[i],
queryIdx = 0,
startValueIdx = 0,
idx = 0;
if (row.trim() === '') { continue; }
while (idx < row.length) {
/* if we meet a double quote we skip until the next one */
var c = row[idx];
if (c === '"') {
do { c = row[++idx]; } while (c !== '"' && idx < row.length - 1);
}
if (c === ',' || /* handle end of line with no comma */ idx === row.length - 1) {
/* we've got a value */
var value = row.substr(startValueIdx, idx - startValueIdx).trim();
/* skip first double quote */
if (value[0] === '"') { value = value.substr(1); }
/* skip last comma */
if (value[value.length - 1] === ',') { value = value.substr(0, value.length - 1); }
/* skip last double quote */
if (value[value.length - 1] === '"') { value = value.substr(0, value.length - 1); }
var key = headers[queryIdx++];
obj[key] = value;
startValueIdx = idx + 1;
}
++idx;
}
result.push(obj);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment