I am creating a project in Node JS and Typescript in which I want to download a CSV with the information that an API contains in JSON format. Given the url http://localhost:3000/?api=api1
, I have to read the JSON related to api1
.
I have added the modules that I have seen that are necessary but I cannot download the CSV from an external JSON by url.
This is my controller
:
import { Request, Response } from 'express';
const converter = require("json-2-csv");
const fetch = require("node-fetch");
const fs = require("fs");
const flatten = require('flat');
const conf = require(`../config/${process.env.NODE_ENV}.json`);
class IndexController {
public async index(req: Request, res: Response) {
const api =req.query.api; //api1
const url = conf.API_MOCS[`${api}`].url; //https://mocks.free.beeceptor.com/api1
const env = process.env.NODE_ENV;
const nameFile = `${env}.${api}.csv`;
let json = await axios.get(url);
const header={
'index':'Index',
'integer':'Integer',
'float': 'Float',
'name': 'Name'};
let json2csvCallback = function (err:any, csv:any) {
if (err) throw err;
const maxRecords = 50;
const headers = csv.split('\n').slice(0,1);
const records = csv.split('\n').slice(0,);
for(let i=1; i<records.length; i=i+maxRecords) {
let dataOut = headers.concat(records.slice(i, i+maxRecords)).join('\n');
let id = Math.floor(i/maxRecords)+1;
fs.writeFileSync(`${dir}/${nameFile}.${id}.csv`, dataOut);
}
};
converter.json2csv(json.data.items, json2csvCallback);
}
}
export const indexController = new IndexController();
In a file I have stored the URL where the information is in JSON, which I read with the variable (url
), how can I download the information from that URL in a CSV file with a maximum of 999 lines and save it in src / output
?
You can call that url using axios or request, after getting the JSON in response you can use https://www.npmjs.com/package/json2csv to convert JSON to csv.
import { Request, Response } from 'express';
const converter = require("json-2-csv");
const fetch = require("node-fetch");
const axios = require('axios');
const fs = require("fs");
const flatten = require('flat');
const conf = require(`../config/${process.env.NODE_ENV}.json`);
class IndexController {
public async index(req: Request, res: Response) {
const api =req.query.api; //api1
const url = conf.API_MOCS[`${api}`].url; //https://mocks.free.beeceptor.com/api1
console.log("Getting JSON ...");
let json = await axios.get("https://mocks.free.beeceptor.com/api1");
console.log("JSON Fetched ...");
const header={
'index':'Index',
'index_start_at':'Index start',
'integer':'Integer',
'float': 'Float',
'name': 'Name'};
converter.json2csv([header,...json.data.items], (err:any, csv:any) => {
if (err) {
throw err;
}
console.log("Making CSV ...");
fs.writeFileSync("file.csv",csv);
console.log("CSV File Is Ready...");
});
}
}
export const indexController = new IndexController();