Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
file: reviewJsonFile,
},
};
// review, company, or job
const typeArg = process.argv[2];
if (!["-c", "-j", "-r"].includes(typeArg)) {
console.log(
"One of '-c', '-j', or '-r' must be specified to indicate which file type to convert!"
);
process.exit(1);
}
const dictEntry = dict[typeArg];
const json2csvParser = new Parser({ fields: dictEntry.fields });
const jsonInput = dictEntry.file;
console.log("Parsing...");
const csvOutput = json2csvParser.parse(jsonInput);
console.log(`Outputting to ./migration/${dictEntry.key}.csv`);
fs.writeFileSync(`./migration/${dictEntry.key}.csv`, csvOutput, () => {});
console.log("Done migration!");
import * as React from "react"
import { Parser } from "json2csv"
const json2csv = new Parser()
export const downloadCSV = (csvName: string, data: any) => {
const CSV = json2csv.parse(data)
const blob = new Blob([CSV], { type: "text/csv" })
const dlink = document.createElement("a")
dlink.download = csvName ? `${csvName.replace(/ /g, "_")}.csv` : "vis.csv"
dlink.href = window.URL.createObjectURL(blob)
dlink.onclick = () => {
// revokeObjectURL needs a delay to work properly
const revokeFn = () => {
window.URL.revokeObjectURL(dlink.href)
}
setTimeout(revokeFn, 1500)
}
parseJsonToCsv: (data, fields) => {
const opts = {fields};
let csv = '';
try {
const parser = new Json2csvParser(opts);
csv = parser.parse(data);
} catch (err) {
console.error(err);
}
return csv;
},
function dataExport(localData, localFileName, fields) {
console.log('--------- Exporting Data : ' + localFileName + '---------')
let json2csvParser = new Json2csvParser({fields});
let csvExport = json2csvParser.parse(localData);
fs.writeFile(localFileName + '.csv', csvExport, function (error) {
if (error) throw error;
});
return;
}
const dataFolder = pathJoin(system.projectFolder, "data", system.startDate);
await mkdirp(dataFolder);
const file = pathJoin(dataFolder, params.file || "data.csv");
const csvConfig = {
header: true,
flatten: true
};
if (!await exists(file)) {
await writeFile(file, "");
} else {
csvConfig.header = false;
}
const parser = new Parser(csvConfig);
const csv = parser.parse(extraction);
const fd = await open(file, "a");
const ap = Buffer.from(`${csv}${EOL}`);
await write(fd, ap, 0, ap.length);
await close(fd);
return input;
}
await this.zipIt();
}
let json = this._filepath ? `${this._filepath}/${this._filename}_${this._date}.json` : `${this._filename}_${this._date}.json`;
let csv = this._filepath ? `${this._filepath}/${this._filename}_${this._date}.csv` : `${this._filename}_${this._date}.csv`;
let zip = this._filepath ? `${this._filepath}/${this._filename}_${this._date}.zip` : `${this._filename}_${this._date}.zip`;
if (this._filetype === 'csv' || this._filetype === 'all'){
switch(this._scrapeType){
case 'user':
case 'hashtag':
case 'location':
this._json2csvParser = new Json2csvParser({ fields: CONST.csvFields });
break;
case 'comments':
this._json2csvParser = new Json2csvParser({ fields: CONST.csvCommentFields });
break;
case 'likers':
case 'followers':
this._json2csvParser = new Json2csvParser({ fields: CONST.csvLikersFields });
break;
}
}
switch(this._filetype){
case 'json':
await Bluebird.fromCallback(cb => fs.writeFile(json, JSON.stringify(this._collector), cb));
break;
case 'csv':
await Bluebird.fromCallback(cb => fs.writeFile(csv, this._json2csvParser.parse(this._collector), cb));
break;
case 'all':
return await lock.acquire('jsonToCSV', async () => {
const parser = new Parser({
fields,
})
return parser.parse(jsObjects)
})
}
}
const harmonyRecords = records.map(n => ({
'Amount': n.amount,
'Asset': n.asset,
'Date & Time': n.created_at,
'Fiat Amount': n.fiat_amount,
'From ID': n.from_id,
'Network ID': n.external_id,
'Notes': n.notes,
'To ID': n.to_id,
'Transaction ID': n.id,
'Type': n.type,
}));
const parser = new Parser({fields});
return {csv: parser.parse(harmonyRecords)};
};
createCSV(fields, data) {
const parser = new Json2csvParser({
fields,
delimiter: ';',
excelStrings: true,
})
return parser.parse(data)
}