Duaer

Snippets

Parse CSV text in Duaer, including GBK files

In Duaer, read CSV files with Extract From CSV; parse CSV text from an API, or a GBK-encoded bank export, into one item per row with the Code node.

Pick the right reader in Duaer

  • A UTF-8 CSV file (form upload, email attachment, drive download): use Extract From CSV in Extract from File. No code.
  • An API that returns CSV: set Response Format to File under Options in HTTP Request, then Extract From CSV.
  • A GBK-encoded export reads as garbage in Extract From CSV: use Extract From Text File in Extract from File, set File Encoding to gbk under Options, and parse the text in data with the code below.

Parse CSV in the Duaer Code node

Add Code, keep Run Once for All Items, and paste:

function parseCsv(text, delimiter = ',') {
  const rows = [];
  let row = [];
  let field = '';
  let inQuotes = false;
  for (let i = 0; i < text.length; i++) {
    const c = text[i];
    if (inQuotes) {
      if (c === '"' && text[i + 1] === '"') { field += '"'; i++; }
      else if (c === '"') inQuotes = false;
      else field += c;
    } else if (c === '"') inQuotes = true;
    else if (c === delimiter) { row.push(field); field = ''; }
    else if (c === '\n' || c === '\r') {
      if (c === '\r' && text[i + 1] === '\n') i++;
      row.push(field); rows.push(row); row = []; field = '';
    } else field += c;
  }
  if (field !== '' || row.length) { row.push(field); rows.push(row); }
  return rows;
}

const out = [];
for (const item of $input.all()) {
  const [header, ...lines] = parseCsv(String(item.json.data).replace(/^\uFEFF/, ''));
  for (const cells of lines) {
    if (cells.every((v) => v === '')) continue;
    out.push({ json: Object.fromEntries(header.map((h, i) => [h.trim(), cells[i] ?? ''])) });
  }
}
return out;

It handles commas and line breaks inside quotes and doubled quotes, uses row one as headers, and skips empty rows. For semicolons pass ";" as the second argument to parseCsv; for tabs pass "\t".

Questions

Why does a CSV export show garbled text in Duaer?

The file is likely GBK. In Duaer, use Extract From Text File with File Encoding gbk, then parse it in the Code node.

Can the Duaer Code node use a library like csv-parse?

By default the Duaer Code node cannot load third-party libraries; use the dependency-free parser on this page.

In this section