Skip to content

Instantly share code, notes, and snippets.

@izaacdb
Last active May 16, 2018 09:49
Show Gist options
  • Save izaacdb/6dd363e81b774e58b178f1f294d0bc7c to your computer and use it in GitHub Desktop.
Save izaacdb/6dd363e81b774e58b178f1f294d0bc7c to your computer and use it in GitHub Desktop.
CSV to JSON in ES6
export const csvToJson = (csv: string) => {
const brands = csv.split('\n') || 'error';
const titles = brands.shift().split(','); //takes first row
const json = brands
.map((b: any) => {
const brand = b.split(',');
return brand
.map(
(x: any, i: any) => {
return { [titles[i]]: x };
}
)
.reduce(
(acc: any, value: any) => {
return Object.assign(acc, value);
},
{}
);
});
console.log(json);
return json;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment