Skip to content

Instantly share code, notes, and snippets.

@pilgreen
Created February 25, 2020 21:31
Show Gist options
  • Save pilgreen/52c5ca9861e1941cf306f5d7c9060570 to your computer and use it in GitHub Desktop.
Save pilgreen/52c5ca9861e1941cf306f5d7c9060570 to your computer and use it in GitHub Desktop.
Simple lightweight CSV conversion to JSON.
function csvToJSON(csv) {
const lines = csv.split('\n')
const result = []
const headers = lines[0].split(',')
for (let i = 1; i < lines.length; i++) {
if (!lines[i])
continue
const obj = {}
const currentline = lines[i].split(',')
for (let j = 0; j < headers.length; j++) {
obj[headers[j]] = currentline[j]
}
result.push(obj)
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment