Parse and Generate CSV#
Read a CSV file from the file system, process its rows, and write a new CSV. This is the shape of most data jobs: read, transform, write. The example totals a column of numbers.
The Script#
// csv.js
import fs from "fs";
const text = await fs.readTextFile("/data/sales.csv");
const lines = text.trim().split("\n");
const header = lines[0].split(",");
const nameIndex = header.indexOf("name");
const amountIndex = header.indexOf("amount");
let total = 0;
const out = ["name,total"];
for (const line of lines.slice(1)) {
const cells = line.split(",");
const name = cells[nameIndex];
const amount = Number(cells[amountIndex]) || 0;
total += amount;
out.push(`${name},${amount}`);
}
out.push(`TOTAL,${total}`);
await fs.mkdir("/reports", { recursive: true });
await fs.writeTextFile("/reports/sales-summary.csv", out.join("\n"));
console.log(`Rows: ${lines.length - 1}, total: ${total}`);
How It Works#
fs.readTextFilereturns the file as a UTF-8 string.trim()drops the trailing newline before splitting into rows.Number(...) || 0treats a blank or invalid cell as zero rather than producingNaN.- The output is built as an array of lines and joined once, which is simple and avoids repeated string concatenation.
This parser splits on commas only. If your data contains quoted fields with commas or newlines, read the file line by line and handle quoting explicitly.
Setup#
Create /data/sales.csv in the workbench, for example:
name,amount
Ada,120
Grace,80
Then create a job that points at csv.js.