-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjsonstat2csv
executable file
·63 lines (59 loc) · 1.89 KB
/
jsonstat2csv
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
61
62
63
#!/usr/bin/env node
var
utils=require("jsonstat-utils"),
argv=require("yargs")
.version()
.usage("Usage:\n $0 [input filename] [output filename]\n $0 < [input] > [output] -t")
.example("$0 oecd.json oecd.csv", "converts JSON-stat file oecd.json into a new file (oecd.csv) in the CSV format.")
.example("$0 < oecd.json > oecd.csv -t", "converts JSON-stat stream oecd.json into a new stream (oecd.csv) in the CSV format.")
.boolean("r")
.alias("r", "rich")
.describe("r", "Create a rich CSV (JSON-stat Comma-Separated Values format)")
.boolean("s")
.alias("s", "status")
.describe("s", "Include status information")
.alias("n", "na")
.describe("n", "Not available text (default, 'n/a')")
.alias("c", "column")
.describe("c", "Column separator (default, ',')")
.alias("u", "unit")
.describe("u", "Unit separator (default, '|'). It must be different from the column separator")
.alias("d", "decimal")
.describe("d", "Decimal separator (default, '.', unless column separator is ';', then default is ',')")
.alias("v", "vlabel")
.describe("v", "Label of the value field (default is 'Value')")
.alias("l", "slabel")
.describe("l", "Label of the status field (default is 'Status')")
.boolean("t")
.alias("t", "stream")
.describe("t", "Enable the stream interface")
.help("h")
.alias("h", "help")
.argv
,
inout=require("./inout"),
callback=function(contents){
var
csv=utils.toCSV(
inout.dataset(contents),
{
rich: argv.rich,
status: argv.status,
vlabel: argv.vlabel,
slabel: argv.slabel,
na: argv.na,
delimiter: (argv.column==="\\t") ? "\t" : argv.column,
separator: (argv.unit==="\\t") ? "\t" : argv.unit,
decimal: argv.decimal
}
)
;
if(csv===null){
console.error("Error: The input does not containt valid JSON-stat.");
process.exit(1);
}else{
return csv;
}
}
;
inout.main(argv, callback, true);