Skip to content

Commit e758dc9

Browse files
Xavier BadosaXavier Badosa
authored andcommitted
v. 0.9.1
jsonstatslice (now also known as jsonstat2jsonstat) had a bug: it didn't work correctly with JSON-stat<2.0.
1 parent 9b070fb commit e758dc9

4 files changed

Lines changed: 128 additions & 8 deletions

File tree

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +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
16+
* [jsonstatslice (aka jsonstat2jsonstat)](#jsonstatslice-aka-jsonstat2jsonstat) - creates JSON-stat from JSON-stat
1717

1818
## Example
1919

@@ -353,9 +353,9 @@ Boolean. Identifies categories by ID instead of label.
353353
jsonstat2object oecd.json oecd-object.json --cid
354354
```
355355

356-
## jsonstatslice
356+
## jsonstatslice (aka jsonstat2jsonstat)
357357

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.
358+
Creates JSON-stat from JSON-stat. It can be used to convert old JSON-stat to JSON-stat version 2.0. Because it supports a filter parameter, it can also be used to 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.
359359

360360
```
361361
jsonstatslice oecd.json oecd-subset.json -f area=DE,year=2014
@@ -365,7 +365,11 @@ In the previous example, oecd-subset.json only contains data for Germany in 2014
365365

366366
#### --filter (-f)
367367

368-
String. Specifies a filter. When no filter is specified, the original JSON-stat will be returned.
368+
String. Specifies a filter. When no filter is specified, the original JSON-stat will be returned unless it was not JSON-stat 2.0: in such case, it will be updated to version 2.0.
369+
370+
```
371+
jsonstat2jsonstat jsonstat10.json jsonstat20.json
372+
```
369373

370374
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.
371375

jsonstat2jsonstat

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env node
2+
3+
var
4+
argv=require("yargs")
5+
.version()
6+
.usage("Usage:\n $0 [input filename] [output filename]\n $0 < [input] > [output] -t")
7+
.example("$0 old.json v20.json", "converts JSON-stat file old.json into JSON-stat 2.0 (v20.json).")
8+
.example("$0 < old.json > v20.json -t", "converts JSON-stat stream old.json into a JSON-stat 2.0 stream (v20.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+
stringify=function(json){
21+
var tree=json.__tree__;
22+
23+
//if v<2.0 convert to 2.0
24+
if(!tree.hasOwnProperty("id")){
25+
tree.version="2.0";
26+
if(!tree.hasOwnProperty("class")){
27+
tree.class="dataset";
28+
}
29+
30+
tree.id=tree.dimension.id;
31+
tree.size=tree.dimension.size;
32+
delete tree.dimension.id;
33+
delete tree.dimension.size;
34+
35+
if(tree.dimension.hasOwnProperty("role")){
36+
tree.role=tree.dimension.role;
37+
delete tree.dimension.role;
38+
}
39+
}
40+
41+
delete tree.role.classification;
42+
["geo", "time", "metric"].forEach(function(e){
43+
if(tree.role[e]===null){
44+
delete tree.role[e];
45+
}
46+
});
47+
48+
return JSON.stringify(tree);
49+
}
50+
51+
callback=function(contents){
52+
var
53+
arrfil,
54+
subset,
55+
filter=argv.filter
56+
;
57+
58+
//filter is not required: if not specified (no -f or -f without string) input=output
59+
if(!filter || filter===true){
60+
//Depending on version (v<2.0)
61+
return contents.id ? contents : stringify( inout.dataset(contents) );
62+
}
63+
64+
if(filter.indexOf("=")===-1){
65+
console.error("Error: The filter has not been specified correctly (missing =).");
66+
process.exit(1);
67+
}
68+
69+
arrfil=filter.split(",").map(function(e){
70+
return e.split("=");
71+
});
72+
73+
subset=inout.dataset(contents).Slice(arrfil);
74+
if(subset===null){
75+
console.error("Error: The input does not containt valid JSON-stat or the filters are not valid.");
76+
process.exit(1);
77+
}else{
78+
return stringify(subset);
79+
}
80+
}
81+
;
82+
83+
inout.main(argv, callback, true);

jsonstatslice

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,37 @@ var
1717
,
1818
inout=require("./inout"),
1919

20+
stringify=function(json){
21+
var tree=json.__tree__;
22+
23+
//if v<2.0 convert to 2.0
24+
if(!tree.hasOwnProperty("id")){
25+
tree.version="2.0";
26+
if(!tree.hasOwnProperty("class")){
27+
tree.class="dataset";
28+
}
29+
30+
tree.id=tree.dimension.id;
31+
tree.size=tree.dimension.size;
32+
delete tree.dimension.id;
33+
delete tree.dimension.size;
34+
35+
if(tree.dimension.hasOwnProperty("role")){
36+
tree.role=tree.dimension.role;
37+
delete tree.dimension.role;
38+
}
39+
}
40+
41+
delete tree.role.classification;
42+
["geo", "time", "metric"].forEach(function(e){
43+
if(tree.role[e]===null){
44+
delete tree.role[e];
45+
}
46+
});
47+
48+
return JSON.stringify(tree);
49+
}
50+
2051
callback=function(contents){
2152
var
2253
arrfil,
@@ -26,7 +57,8 @@ var
2657

2758
//filter is not required: if not specified (no -f or -f without string) input=output
2859
if(!filter || filter===true){
29-
return contents;
60+
//Depending on version (v<2.0)
61+
return contents.id ? contents : stringify( inout.dataset(contents) );
3062
}
3163

3264
if(filter.indexOf("=")===-1){
@@ -43,7 +75,7 @@ var
4375
console.error("Error: The input does not containt valid JSON-stat or the filters are not valid.");
4476
process.exit(1);
4577
}else{
46-
return JSON.stringify(subset.__tree__);
78+
return stringify(subset);
4779
}
4880
}
4981
;

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.9.0",
3+
"version": "0.9.1",
44
"description": "JSON-stat Command Line Conversion Tools",
55
"homepage": "https://github.com/badosa/JSON-stat-conv",
66
"keywords": [
@@ -18,7 +18,8 @@
1818
"jsonstat2object": "jsonstat2object",
1919
"jsonstat2csv": "jsonstat2csv",
2020
"csv2jsonstat": "csv2jsonstat",
21-
"jsonstatslice": "jsonstatslice"
21+
"jsonstatslice": "jsonstatslice",
22+
"jsonstat2jsonstat": "jsonstat2jsonstat"
2223
},
2324
"scripts": {
2425
"test": "echo \"Error: no test specified\" && exit 1"

0 commit comments

Comments
 (0)