Melodie
Melodie

Reputation: 325

How to use @json2csv/node transforms

I am trying to follow this doc, below is my code.

const { createReadStream, createWriteStream } = require("fs");
const { Transform } = require("@json2csv/node");
const Stream = require("stream");

const input = createReadStream("testinput.csv", { encoding: "utf8" });
const output = createWriteStream(`testoutput.csv`, { encoding: "utf8" });
const parser = new Transform({ fields: ["Date_time", "ch1_data_P"] });
parser
.on("line", (line) => {
    console.log(line);
})
.on("header", (header) => {
    console.log(header);
})
.on("error", (err) => console.error(err));
input.pipe(parser).pipe(output);

The input.csv has content below, which is proper as far as I know

"Date_time","ch1_data_P"
"2023-06-08T00:00:00Z",42.1
"2023-06-09T00:00:00Z",41.3
"2023-06-10T00:00:00Z",43.234

However, when I run this code, I get the error:

Error: Data items should be objects or the "fields" option should be included
at tokenizer.onToken (D:\megha\Documents\Projects\Hydrometeo\access2postgres\node_modules\@json2csv\plainjs\dist\cjs\StreamParser.js:74:30)
at Tokenizer.write (D:\megha\Documents\Projects\Hydrometeo\access2postgres\node_modules\@streamparser\json\dist\cjs\tokenizer.js:245:34)
at JSON2CSVStreamParser.write (D:\megha\Documents\Projects\Hydrometeo\access2postgres\node_modules\@json2csv\plainjs\dist\cjs\StreamParser.js:91:24)
at JSON2CSVNodeTransform._transform (D:\megha\Documents\Projects\Hydrometeo\access2postgres\node_modules\@json2csv\node\dist\cjs\Transform.js:33:31)
at Transform._write (node:internal/streams/transform:175:8)
at writeOrBuffer (node:internal/streams/writable:392:12)
at _write (node:internal/streams/writable:333:10)
at Writable.write (node:internal/streams/writable:337:10)
at ReadStream.ondata (node:internal/streams/readable:766:22)
at ReadStream.emit (node:events:513:28)

I couldn't find any example in the doc with the fields, so I gave it my best try. There is also no information on the content of the input file in the docs.

How do I output a csv file? Eventually, I want to output a csv file from a json array of objects in memory, but I need to get the basic example working first.

Upvotes: 1

Views: 1186

Answers (1)

MWY
MWY

Reputation: 1181

I suspect the error you're getting is due to a misunderstanding of the packages you're using. @json2csv/node package is not for reading existing he CSV data, but for converting JSON data to CSV. This is why the error message says "Data item must be an object", indicating that you want his JSON object instead of the CSV row. 

so, I write an example for you:


const { createWriteStream } = require("fs");
const { Transform } = require("@json2csv/node");

let data = [
  {
    "Date_time": "2023-06-08T00:00:00Z",
    "ch1_data_P": 42.1
  },
  {
    "Date_time": "2023-06-09T00:00:00Z",
    "ch1_data_P": 41.3
  },
  {
    "Date_time": "2023-06-10T00:00:00Z",
    "ch1_data_P": 43.234
  }
];

const json2csv = new Transform({
  fields: ["Date_time", "ch1_data_P"],
});

const output = createWriteStream("testoutput.csv", { encoding: "utf8" });

json2csv.pipe(output);

data.forEach((item) => {
  output.write(`${item.Date_time},${item.ch1_data_P}\n`);
});


json2csv.end();

Upvotes: 0

Related Questions