-
Notifications
You must be signed in to change notification settings - Fork 0
/
json-to-csv.ts
60 lines (48 loc) · 1.59 KB
/
json-to-csv.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { getArgs } from "./helper";
import * as jsonfile from "jsonfile";
import * as fs from "fs";
const usageString: string = `
Args: node dist/utils/json-to-csv.js --json <bla.json> --csv <bla.csv>
\n
Ex: node dist/utils/json-to-csv.js --json bla.json --csv bla.csv
\n
\n
Parameters: \n
\t - json: JSON input
\t - csv: CSV output
`;
const args: any = getArgs(usageString);
const hashLines: any = {};
const separator = "__"
main().then(() => console.log("Foi")).catch((err) => console.log("Deu erro", err))
async function main() {
const JSONInput: any = jsonfile.readFileSync(args["json"]);
const csvFile = args["csv"];
transformJSONIntoCSV(JSONInput, "")
let csv = "Key, Value\n";
Object.keys(hashLines).forEach(key =>
csv += `${key}, ${typeof hashLines[key] == "string" ? hashLines[key].replace(/,/g, "<comma>").replace(/\n/g, "<newline>") : hashLines[key]}\n`
)
fs.writeFileSync(csvFile, csv)
}
function transformJSONIntoCSV(JSONInput: any, headerString: string) {
for (const key of Object.keys(JSONInput)) {
const myHeaderString: string = headerString ? headerString + separator + key : key;
const value = JSONInput[key];
if (!value) {
hashLines[myHeaderString] = value;
}
// Caso quando eh objeto (obj ou array)
else if (typeof value == "object") {
// Array
if (value.length != undefined)
value.forEach((element: any, i: number) => transformJSONIntoCSV(element, myHeaderString + separator + i))
// Objeto
else
transformJSONIntoCSV(value, myHeaderString)
}
else {
hashLines[myHeaderString] = value;
}
}
}