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.

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)

Showcase builds

19 complete workflows from my own projects, each with its n8n workflow JSON to import. Showcase entries link the file at the end of the article.

See the showcase builds

Keep reading

191 entries grouped by topic, from first workflow to queue mode. Free, no signup.

Browse the encyclopedia

Need it built?

I design, build and run n8n systems for clients. Every engagement starts with a $1,500 diagnostic audit, credited toward the build.

Book a 20-minute call