gburnett
gburnett

Reputation: 785

How to create an JSON array using node-csvtojson?

When using node-csvtojson how can I output data as a json array?

const request = require("request");
const csv = require("csvtojson");
const fs = require("fs");

const readStream = request.get("http://www.example.com/some.csv");
const writeStream = fs.createWriteStream("./some.json");
const converter = csv({
  includeColumns: /Column3|Column2/
});

readStream.pipe(converter).pipe(writeStream);

Assuming the following data structure:

Column1,Column2,Column3
1,2,3
one,two,three

I would expect the output file to contain:

[
  { "Column2": "2", "Column3": "3" },
  { "Column2": "two", "Column3": "three" }
]

I actually get the following:

{ "Column2": "2", "Column3": "3" }
{ "Column2": "two", "Column3": "three" }

How to I ensure that I get a JSON array as output?

I am aware of this issue but I notice that toArrayString has been removed from the api.

Upvotes: 0

Views: 625

Answers (2)

Keyang
Keyang

Reputation: 1878

Just a late response on this. In version 2.0.10 the JSON array format is back.

Simply set downstreamFormat to json

csv({downstreamFormat:"json"}).fromFile(fileName).pipe(fileWritableStream);

Details can be found here

Upvotes: 1

gburnett
gburnett

Reputation: 785

At the time of asking I am unable to do what I want with the csvtojson api.

In case anybody is interested my eventual solution was to use through2-map and through2-reduce to create an array.

...

const map = require("through2-map");
const reduce = require("through2-reduce");

...

readStream
  .pipe(converter)
  .pipe(reduce((previous, current) => {
    return previous ? `${previous},${current}` : current;
  })
  .pipe(map(chunk => `[${chunk}]`))
  .pipe(writeStream);

Upvotes: 0

Related Questions