3

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.

sirtz
  • 75
  • 2
  • 9
  • (1) Use your favorite fetch library to get JSON from the API (2) Parse the JSON into an object (`JSON.parse()`) (3) Call `Papa.unparse(yourData)`? – AKX Jun 14 '21 at 07:58
  • (ps.: there's no reason to make a class for a single function, and similarly `new constUtils().maxRecords` should probably just be a `consts.maxRecords`.) – AKX Jun 14 '21 at 07:59
  • @AKX Yes, I can have it in the same function. From the conversion I understand how the whole process works, my problem is that I cannot use Papaparse in this case, to convert a JSON from an api into a CSV. The ``converterUtils`` variable is the one that contains the data. – sirtz Jun 14 '21 at 08:03
  • _Why_ cannot you? What have you tried? As my first comment says: get the data, parse it from JSON, pass it to Papa. – AKX Jun 14 '21 at 08:04
  • @AKX 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. – sirtz Jun 14 '21 at 08:26

1 Answers1

2

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);
AKX
  • 152,115
  • 15
  • 115
  • 172