Parsing CSV data isn’t as easy as splitting the string up into individual lines,
then splitting it by the delimited (in this case commas). Some of the values may
have quotes around them, sometimes the values themselves have line breaks. Then
there’s the potential that the data itself is malformed.
Since handling CSV tends to be a pain, I always reach for an existing library to
do the dirty work. Like many of the more common programming tasks, CSV parsing
libraries are in an over abundance.
While I’ve used many of them over the years, I currently reach for
papaparse
as they tout being one of the fastest, but they also handling
going from JSON back to CSV, which is something else I do on a fairly regular
basis.
With over 1,000,000 weekly installs, it’s become quite popular and worth a look
if you haven’t checked it out before.
To install papaparse
to your project, simply use npm
or yarn
:
npm install --save papaparse
# or
yarn add papaparse
With the dependency installed, you’ll want to require
or import
it and then
coax it into working it’s magic with the CSV data. If you already have the CSV
data loaded up, you can ignore the fs
logic below:
have available):
const fs = require('fs'
const Papa = require('papaparse'
const file = '/path/to/your/file.csv'
const csvData = fs.readFileSync(file, 'utf8'
const jsonData = Papa.parse(csvData, { header: true
Not a whole heck of a lot to it! The inclusion of the header: true
part of the
options ensures we receive a well-formed object that uses the header row as the
object properties:
[
{
header1: 'like',
header2: 'this'
}
]
Obviously this recipe is making a hard assumption that your file is small enough
to load in. If it’s not, no big deal, papaparse
supports streaming large files
and will even accept a URL instead of the raw CSV data like we did above.