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.
Via comments:
These are the variables I use to make the call:
let api = req.query.api; let url = this.constUt.conf.API_MOCS [`${api}`].url; let json = await axios.get (url); Papa.unparse (json);
In this case Stand Up does nothing.
axios.get
returns a response object; the JSON data is available as data
.
You can't just pass the full response object to Papa and expect sane results.
const url = this.constUt.conf.API_MOCS[String(req.query.api)].url;
const resp = await axios.get(url);
const data = Papa.unparse(resp.data);
console.log(data);