CSV String to Array in JavaScript

Feb 24, 2009

I love using comma-separated value (CSV) strings in JavaScript. They are faster to parse than JSON objects and take a lot less time and space to transfer. However, while parsing CSV strings is easy when all you're dealing with is text and commas, things get a little more complicated when you start throwing in quoted values which themselves contain commas. I recently had this problem when the data being sent to a script I was working on suddently needed to become more complex.

Simply using the JavaScript .split([i][separator][/i]) method of the String object, where the separator is a comma, will work on data such as these:


... but it will fail on data like this:

"hello, how are you?","I am fine, thanks","That's good"

Using .split(",") on that string will give you this output:

 how are you?"
"I am fine
"That's good"

Definitely not desirable output. So I browsed around the Internet for a suitable replacement function I could just drop in, and unsurprisingly I could not find anything that was a) uncomplicated and b) sufficiently resilient towards "broken" CSV strings.

Of course then, I had to make my own :)

String.prototype.splitCSV = function(sep) {
  for (var foo = this.split(sep = sep || ","), x = foo.length - 1, tl; x >= 0; x--) {
    if (foo[x].replace(/"\s+$/, '"').charAt(foo[x].length - 1) == '"') {
      if ((tl = foo[x].replace(/^\s+"/, '"')).length > 1 && tl.charAt(0) == '"') {
        foo[x] = foo[x].replace(/^\s*"|"\s*$/g, '').replace(/""/g, '"');
      } else if (x) {
        foo.splice(x - 1, 2, [foo[x - 1], foo[x]].join(sep));
      } else foo = foo.shift().split(sep).concat(foo);
    } else foo[x].replace(/""/g, '"');
  } return foo;

The code above adds a prototype method to the String object so all created Strings will have the splitCSV() method available to them. The method takes a separator argument (comma by default) and works by first doing a general split on all commas, and then merging groups of quoted strings back into single values.

To use it, just use the method on the string you wish to parse, like so:

var mystring = 'this,is,"some, sample","csv, text, for",you,to,"look",at';
var parsed = mystring.splitCSV();

It handles all sorts of broken and unusual CSV, like empty quoted values (...,"",...), VB-style double quotation marks (...,"A ""lovely"" day!",...), newlines in values (...,Happy Happy\nJoy Joy!,...) and even invalid spaces between commas and quotes (..., "Oopsie!" ,...)

Maybe it can be useful to you too ;)

The Benefits of Targetted CSS So, what happened to 2008?

Comments closed

Recent posts

  1. What I'd Like To See In The Elder Scrolls VI - Part 2 Aug 2015
  2. What I'd Like To See In The Elder Scrolls VI Jul 2015
  3. Cyprus, and what capitalists want Mar 2013
  4. Let interest rates on housing rise Sep 2012
  5. Low carb mashed cauliflower with avocado Jan 2012
  6. Archive

Items of Interest

Webcomics Reading List

Good Eats

Twitter Google+ RSS 2.0 Valid XHTML 1.0! Copyright © 2015 Brian Huisman AKA GreyWyvern
ContactSite mapSearch