Loading CSV files in JS: Recommended methods?

Loading CSV files in JS: Recommended methods?
0.0 0

#1

Hi, I’m looking to load/parse CSV files to JS vars for manipulation. I currently have a working method via a JQuery $.get() call.

However I wondered if anyone had any alternative methods they might suggest? In a certain sense, a synchronous rather than asynchronous call would be somewhat preferred.


#2

If you are already using jQuery, it looks like there’s a jQuery CSV parser plugin. I found it referenced on stack overflow.


#3

I will take a look-- Not necessarily tied to JQuery, just the solution I started with. Was curious what other alternatives there were people had used.


#4

#5

The parsing would usually be synchronous (unless you’re parsing huge files using service workers), but the loading would usually be asynchronous. I’d strongly advise you to reconsider if you’re thinking about loading synchronously — generally not a good idea if you care even slightly about user experience.

In any case, $.get() doesn’t have the ability to parse CSV files.

If this is for learning purposes, I’d recommend you first try writing your own parsing function. CSV is a relatively simple spec, so it’s a good challenge to set yourself as a learner.

If it’s for production code, on the other hand, I’d recommend just using whichever CSV-parsing library has the most stars on github or the most daily downloads on NPM.


#6

You didn’t mention what you’re doing this for, but if you happen to be in the D3 projects, there’s a D3 helper method for fetching and parsing CSV files in the d3-fetch module.