Skip to content

Instantly share code, notes, and snippets.

@tonis2
Created May 14, 2018 12:48
Show Gist options
  • Select an option

  • Save tonis2/e5ddfad010487002590e64ab6efe3b6a to your computer and use it in GitHub Desktop.

Select an option

Save tonis2/e5ddfad010487002590e64ab6efe3b6a to your computer and use it in GitHub Desktop.
CSV data to JSON
const csvToJson = csv => {
const response = [];
const allLines = csv.split(/\r\n|\n/);
const headers = allLines[0].split(/\t|,/).filter(value => value);
for (let item = 1; item < allLines.length; item++) {
const data = {};
const lineData = allLines[item].split(/\t|,/).filter(value => value);
if (lineData.length == headers.length)
lineData.forEach((line, index) => {
data[headers[index]] = line;
});
response.push(data);
}
return response;
};
export default csvToJson;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment