Skip to content

Instantly share code, notes, and snippets.

@lucasdu4rte
Forked from iwek/csv-to-json.js
Last active April 2, 2018 13:31
Show Gist options
  • Save lucasdu4rte/d95dc7fd12d2ad8873364ed59dee019b to your computer and use it in GitHub Desktop.
Save lucasdu4rte/d95dc7fd12d2ad8873364ed59dee019b to your computer and use it in GitHub Desktop.
CSV to JSON Conversion in JavaScript
var csvJSON = function(csv){
var lines = csv.split("\n")
var result = []
var headers = lines[0].split(",")
lines.map(function(line, indexLine){
if (indexLine < 1) return // Jump header line
var obj = {}
var currentline = line.split(",")
headers.map(function(header, indexHeader){
obj[header] = currentline[indexHeader]
})
result.push(obj)
})
result.pop() // remove the last item because undefined values
return result //JavaScript object
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment