-
Notifications
You must be signed in to change notification settings - Fork 1
/
jsonstat2csv
executable file
·81 lines (77 loc) · 2.66 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env node
var
utils=require("jsonstat-suite"),
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("f")
.alias("f", "fid")
.describe("f", "Use field IDs instead of labels.")
.boolean("o")
.alias("o", "cid")
.describe("o", "Use content IDs instead of labels.")
.alias("y", "by")
.describe("y", "Dimension to transposed by")
.alias("p", "drop")
.describe("p", "Comma-separated list of constant dimensions to remove when a valid 'by' is specified.")
.boolean("b")
.alias("b", "big")
.describe("b", "This is an emergency option only to be used when the stream interface is not enabled and the dataset is very big.")
.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),
{
array: !argv.stream && argv.big, //big datasets experimentally when interface is not stream
rich: argv.rich,
status: argv.status,
vlabel: argv.vlabel,
slabel: argv.slabel,
field: argv.fid ? "id" : "label",
content: argv.cid ? "id" : "label",
na: argv.na,
delimiter: (argv.column==="\\t") ? "\t" : argv.column,
separator: (argv.unit==="\\t") ? "\t" : argv.unit,
decimal: argv.decimal,
by: argv.by,
drop: argv.drop ? argv.drop.split(",") : []
}
)
;
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);