GreyWyvern.com

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:

1,2,3,4,5
text1,text2,text3,text4
John,Q,Public,Esq.

... 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:

"hello
 how are you?"
"I am fine
 thanks"
"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();
alert(parsed.join("\n"));

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. Cyprus, and what capitalists want Mar 2013
  2. Let interest rates on housing rise Sep 2012
  3. Low carb mashed cauliflower with avocado Jan 2012
  4. The Zalman Odyssey Sep 2011
  5. New pants, new perspective Sep 2011
  6. Archive

Items of Interest

Webcomics Reading List

Good Eats

Twitter Identi.ca Google+ RSS 2.0 Valid XHTML 1.0! Copyright © 2014 Brian Huisman AKA GreyWyvern
ContactSite mapSearch