CSV Parser in JavaScript

Today I needed to process a CSV file, filtering items into two separate lists while expanding certain comma separated cells.
Since this needed to be done often and by several people, I decided to use JavaScript, the FileReader API and WebWorkers, all hosted on a local server.

But JavaScript has no CSV parser, so I set out making one. After making it, I realised lots of people could benefit from it. So here it is.

This is how it works:

  1. Split the file into lines
  2. Split the lines by commas into cells
  3. Re-connect cells that had commas in their values

And, finally, what you’ve come here for, the code:
(copy-pastable: no line numbers, preserves tabs)

function parseCSV(rawData) {
	var rawRows = rawData.split("n");
	var csv = [];
	var row = [];
	for (var rowIndex = 0; rowIndex < rawRows.length; rowIndex++) {
		var currentRow = rawRows[rowIndex].split(",");
		var cell = "";
		var in_quotes = false;
		for (var cellIndex = 0; cellIndex < currentRow.length; cellIndex++) {
			var currentCell = currentRow[cellIndex];
			var quote_start = currentCell.search('"') == 0; // Starts with a quote
			var quote_end = currentCell.substr(1).search('"') == currentCell.length - 2; // Ends with a quote
			if (quote_start && quote_end) { // The cell starts and ends with quotes. It's a single cell.
				cell = currentCell.substr(1, currentCell.length - 2);
			} else if (quote_start) { // The cell is the beginning of several values.
				in_quotes = true;
				cell = currentCell.substr(1) + ",";
			} else if (quote_end) { // The cell is the end of several values.
				in_quotes = false;
				cell += currentCell.substr(0, currentCell.length - 1);
			} else if (in_quotes) { // The cell is in between the start and end quoted cells.
				cell += currentCell + ",";
			} else { // Just a regular, unquoted cell.
				cell = currentCell;
			}
			if (in_quotes == false) { // Only add the cell if we have all the values (ie. not inside quotes)
				row.push(cell);
				cell = ""; // Reset the cell for the next run.
			}
		}
		csv.push(row);
		row = []; // Reset the row for the next run.
	}
	return csv;
}

I hope you find it useful!

License: Do whatever you want with it, public domain :D

Go on, tell me I could have done it with regex, but what would be the fun in that? (Actually I could neither find a usable regex for splitting a csv, nor make one myself. My regex-fu is weak.)

One thought on “CSV Parser in JavaScript”

  1. Cool, thanks for sharing! Any chance you can share the related FileReader and WebWorker code you used with this, as well?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>