I have a Node JS
and Typescript
project in which I currently receive a JSON from an API and convert it into CSV format with a limit of lines, using the json-2-csv
module. I have read that the papaparse
module is quite fast for these things and I would like to know how it works but I cannot use it to convert a JSON
from an api
.
This is the call I make with json-2-csv
:
await this.csvConverter.jsonToCsv(converterUtils, nameFile, this.dir);
This is the class
that handles the conversion:
export class csvConverter {
public async jsonToCsv (csv: any, nameFile: string, dir: string):Promise <number> {
const max: number = new constUtils().maxRecords;
const headers = csv.split('\n').slice(0, 1);
const records = csv.split('\n').slice(0,);
for (let i = 1; i < records.length; i = i + max) {
let dataOut = headers.concat(records.slice(i, i + max)).join('\n');
let id = Math.floor(i / max) + 1;
fs.writeFileSync(`${dir}/${nameFile}.${id}.csv`, dataOut);
}
return Promise.resolve(csv);
};
}
My problem is that I can't do the same with papaparse, I've looked at the documentation but I can convert JSON from local files but I can't get it from an API.