Tips > Building Workflows

Parse CSV and XML Strings Inside Code Nodes

n8n has built-in CSV and XML support, but it fails on malformed data, non-standard delimiters, or XML with namespaces.

TipIntermediate2 min read

n8n has built-in CSV and XML support, but it fails on malformed data, non-standard delimiters, or XML with namespaces. A Code node lets you handle edge cases with full control over parsing logic.

Real-world example: A vendor sends daily inventory updates as a CSV attachment where some fields contain commas inside quotes, the delimiter is a semicolon, and there is a two-line header that the Spreadsheet File node chokes on.

// Mode: Run Once for All Items
const csvString = $input.first().json.csvData;

// Skip the first two header lines, use semicolon delimiter
const lines = csvString.split('\n').slice(2).filter(l => l.trim());

function parseCSVLine(line, delimiter = ';') {
  const result = [];
  let current = '';
  let inQuotes = false;

  for (const char of line) {
    if (char === '"') {
      inQuotes = !inQuotes;
    } else if (char === delimiter && !inQuotes) {
      result.push(current.trim());
      current = '';
    } else {
      current += char;
    }
  }
  result.push(current.trim());
  return result;
}

const headers = ['sku', 'name', 'quantity', 'warehouse', 'lastUpdated'];

return lines.map(line => {
  const values = parseCSVLine(line);
  const obj = {};
  headers.forEach((h, i) => { obj[h] = values[i] ?? ''; });
  obj.quantity = parseInt(obj.quantity, 10) || 0;
  return { json: obj };
});
```text
> **Warning: Large Files**
>
> For CSV files larger than 10 MB, consider processing them in chunks using the Split In Batches node upstream, or switch to `filesystem` binary mode (see the Performance tips).

**Related:** [Use Edit Fields in "Map Each" Mode for Simple Renames](../data-transformation/01-use-edit-fields-in-map-each-mode-for-simple-renames.md) | [Configure Payload Size and Binary Data Mode for Large Files](../performance-and-large-files/01-configure-payload-size-and-binary-data-mode-for-large-files.md)

Want this running in your stack?

I build production n8n and Cloudflare automation for teams — the same engineering behind HarperFlow. Fixed-price, escrow-protected, US-based.