I am using the following function to parse a csv file.
export default function readCsv (csv, reviver) {
reviver = reviver || function(r, c, v) {
return v;
};
let chars = csv.split(''),
c = 0,
cc = chars.length,
start, end, table = [],
row;
while (c < cc) {
table.push(row = []);
while (c < cc && '\r' !== chars[c] && '\n' !== chars[c]) {
start = end = c;
if ('"' === chars[c]) {
start = end = ++c;
while (c < cc) {
if ('"' === chars[c]) {
if ('"' !== chars[c + 1]) {
break;
} else {
chars[++c] = '';
} // unescape ""
}
end = ++c;
}
if ('"' === chars[c]) {
++c;
}
while (c < cc && '\r' !== chars[c] && '\n' !== chars[c] && ',' !== chars[c]) {
++c;
}
} else {
while (c < cc && '\r' !== chars[c] && '\n' !== chars[c] && ',' !== chars[c]) {
end = ++c;
}
}
row.push(reviver(table.length - 1, row.length, chars.slice(start, end).join('')));
if (',' === chars[c]) {
++c;
}
}
if ('\r' === chars[c]) {
++c;
}
if ('\n' === chars[c]) {
++c;
}
}
return table;
}
The json looks like this:
What I want the json to look like is as follows:
[
doc_id: "16278",
framework_id: "8078",
...
],
[
doc_id: "16261",
framework_id: "880",
...
],
Basically, instead of getting the first row's content as the first value in the json, the first row should be converted into keys and rest of the rows into values.