Skip to content

Commit 9b070fb

Browse files
Xavier BadosaXavier Badosa
authored andcommitted
V. 0.9.0
jsonstatslice added
1 parent 12bdf0b commit 9b070fb

3 files changed

Lines changed: 81 additions & 2 deletions

File tree

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Available commands:
1313
* [jsonstat2arrobj](#jsonstat2arrobj) - converts JSON-stat into an array of objects
1414
* [jsonstat2csv](#jsonstat2csv) - converts JSON-stat into CSV
1515
* [jsonstat2object](#jsonstat2object) - converts JSON-stat into an object
16+
* [jsonstatslice](#jsonstatslice) - creates a subset from JSON-stat
1617

1718
## Example
1819

@@ -351,3 +352,29 @@ Boolean. Identifies categories by ID instead of label.
351352
```
352353
jsonstat2object oecd.json oecd-object.json --cid
353354
```
355+
356+
## jsonstatslice
357+
358+
Creates a JSON-stat subset from a JSON-stat dataset. A JSON-stat subset has the same dimensions as the original dataset but with some of them fixed for a certain category.
359+
360+
```
361+
jsonstatslice oecd.json oecd-subset.json -f area=DE,year=2014
362+
```
363+
364+
In the previous example, oecd-subset.json only contains data for Germany in 2014.
365+
366+
#### --filter (-f)
367+
368+
String. Specifies a filter. When no filter is specified, the original JSON-stat will be returned.
369+
370+
A filter is a comma-separated list of selection criteria. Each criterion must follow the pattern *{dimension id}={category id}*. Because dimension ids and category ids are strings that can contain whitespaces, they should be double-quoted.
371+
372+
```
373+
"area"="DE","year"="2014"
374+
```
375+
376+
forces the subset to keep only category "DE" from dimension "area" and category "2014" from dimension "year". Because these ids do not contain whitespaces, double quotes are not strictly necessary.
377+
378+
```
379+
area=DE,year=2014
380+
```

jsonstatslice

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env node
2+
3+
var
4+
argv=require("yargs")
5+
.version()
6+
.usage("Usage:\n $0 [input filename] [output filename] -f [filter]\n $0 < [input] > [output] -f [filter] -t")
7+
.example("$0 oecd.json oecd-subset.json -f \"area\"=\"DE\",\"year\"=\"2014\"", "converts JSON-stat file oecd.json into a new JSON-stat file (oecd-subset.json).")
8+
.example("$0 < oecd.json > oecd-subset.json -f \"area\"=\"DE\",\"year\"=\"2014\" -t", "converts JSON-stat stream oecd.json into a new JSON-stat stream (oecd-subset.json).")
9+
.alias("f", "filter")
10+
.describe("f", "Filter string (for example: area=DE,year=2014)")
11+
.boolean("t")
12+
.alias("t", "stream")
13+
.describe("t", "Enable the stream interface")
14+
.help("h")
15+
.alias("h", "help")
16+
.argv
17+
,
18+
inout=require("./inout"),
19+
20+
callback=function(contents){
21+
var
22+
arrfil,
23+
subset,
24+
filter=argv.filter
25+
;
26+
27+
//filter is not required: if not specified (no -f or -f without string) input=output
28+
if(!filter || filter===true){
29+
return contents;
30+
}
31+
32+
if(filter.indexOf("=")===-1){
33+
console.error("Error: The filter has not been specified correctly (missing =).");
34+
process.exit(1);
35+
}
36+
37+
arrfil=filter.split(",").map(function(e){
38+
return e.split("=");
39+
});
40+
41+
subset=inout.dataset(contents).Slice(arrfil);
42+
if(subset===null){
43+
console.error("Error: The input does not containt valid JSON-stat or the filters are not valid.");
44+
process.exit(1);
45+
}else{
46+
return JSON.stringify(subset.__tree__);
47+
}
48+
}
49+
;
50+
51+
inout.main(argv, callback, true);

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsonstat-conv",
3-
"version": "0.8.3",
3+
"version": "0.9.0",
44
"description": "JSON-stat Command Line Conversion Tools",
55
"homepage": "https://github.com/badosa/JSON-stat-conv",
66
"keywords": [
@@ -17,7 +17,8 @@
1717
"jsonstat2arrobj": "jsonstat2arrobj",
1818
"jsonstat2object": "jsonstat2object",
1919
"jsonstat2csv": "jsonstat2csv",
20-
"csv2jsonstat": "csv2jsonstat"
20+
"csv2jsonstat": "csv2jsonstat",
21+
"jsonstatslice": "jsonstatslice"
2122
},
2223
"scripts": {
2324
"test": "echo \"Error: no test specified\" && exit 1"

0 commit comments

Comments
 (0)