åå ã¯ãJSON ãã¡ã¤ã«ãæ±ãæ¹æ³ãã¾ã¨ãã¾ããã
ä»åã¯ãJSONãã¡ã¤ã«ãæ±ãCUIãã¼ã«ãjqãã«ã¤ãã¦ç´¹ä»ãã¾ãã
ã¯ããã«
ååã®è¨äºã§ãã
daisuke20240310.hatenablog.com
e-Stat ãããã¦ã³ãã¼ãããã令åï¼å¹´å½å¢èª¿æ»ã®ç·å¥³å¥äººå£ã® JSONãã¡ã¤ã«ãã使ãã¾ãã
jqã®ã¤ã³ã¹ãã¼ã«
ãjqãã®ã¤ã³ã¹ãã¼ã«æ¹æ³ã¨ãã¦ãUbuntu 22.04 ã®å ´åã¨ãWindows10 ã® Git BASH ã§ä½¿ãå ´åã®2éããç´¹ä»ãã¾ãã
Ubuntu 22.04
ã¨ã¦ãç°¡åã«ã¤ã³ã¹ãã¼ã«ã§ãã¾ãã
$ sudo apt install jq
Windows10ã®Git BASHã§jqã使ããããã«ãã
ã¾ããWindows10 ç¨ã®ãjqãããã¦ã³ãã¼ããã¾ãã
ãjqãã® GitHub ã§ãã
github.com
ãªãªã¼ã¹ãã¼ã¸ ãããææ°ã® Windows ç¨ã® exeï¼jq-win64.exeï¼ããã¦ã³ãã¼ããã¾ãã
é ãã¦ããããããªãã®ã§ããShow all 29 assetsããã¯ãªãã¯ãã¦ãå
¨ã¦è¡¨ç¤ºãã¦ããã¦ã³ãã¼ããã¾ãã
é©å½ãªå ´æã«ç½®ãã¦ãã³ãã³ãããã³ããããã使ããã¨æãã¾ãããä»åã¯ãGit BASH ã§ä½¿ããããã«ããã¡ã¤ã«ãé
ç½®ãã¾ãã
Git BASH ã¯ãGit for Windows ãã¤ã³ã¹ãã¼ã«ããã¨ä¸ç·ã«å
¥ãã³ã³ã½ã¼ã«ã§ãã
Git for Windows ãã¤ã³ã¹ãã¼ã«ãããã©ã«ããC:\Program Files\Gitãã®ä¸ã«ãusr/binããã£ã¬ã¯ããªãããã®ã§ãããã« jq-win64.exe ãã³ãã¼ãã¦ãjq.exe ã«ãªãã¼ã ãã¦ããã¾ãã
Git BASH ãã使ã£ã¦ã¿ã¾ãã
ç¡äºä½¿ããããã«ãªãã¾ããã
jqã®ä½¿ãæ¹
å
¬å¼ã®ããã¥ã¡ã³ãã§ãã
jqlang.github.io
ãjqãã®åºæ¬çãªä½¿ãæ¹ã¯ã以ä¸ã®2éãã§ãé常ã®ã³ãã³ãã®ããã«ããjq ãã£ã«ã¿ JSONãã¡ã¤ã«ãã®å½¢ã§å®è¡ãããããã¤ãã§ã¤ãªãã§ãã£ã«ã¿ã¨ãã¦ä½¿ããã§ãã
å¾è
ã¯ãã.ããçç¥ãããã¨ãåºæ¥ãããã§ããããã§ã¯ã以éã¯ãå¾è
ã®ä½¿ãæ¹ã¨ãã¾ãã
$ jq . getStatsData.json
$ cat getStatsData.json | jq .
ã¡ãªã¿ã«ãã.ãã¯ãããã¥ã¡ã³ãã«ããã¨ãä½ãããªããã£ã«ã¿ã¨ããæãã§ãåãåã£ããã®ãå
¨ã¦åºåããã¨ããæãã§ãã
大ãããã¡ã¤ã«ãæ±ãå ´åã¯ãless ã³ãã³ããªã©ãå
¥ãã¦ãããæ¹ãããã§ããã
$ cat getStatsData.json | jq | less
jqã®åºæ¬çãªä½¿ãæ¹
e-Stat ã®ãã¼ã¿ã¯ãæåã«ãã¦ã¯é£ããã®ã§ãç°¡åãªãã¼ã¿ãç¨æãã¾ããã
{
"ka0": {
"ka1": "va1",
"kb1": "vb1"
},
"kb0": [
{ "kc1": "vc1" },
{ "kd1": "vd1" }
]
}
sample1.json ã¨ãã¦ãä¿åãã¦ããã¾ãã
ã¾ãã¯ãä½ãããªããã£ã«ã¿ã使ãã¾ãã
$ cat sample1.json | jq .
{
"ka0": {
"ka1": "va1",
"kb1": "vb1"
},
"kb0": [
{
"kc1": "vc1"
},
{
"kd1": "vd1"
}
]
}
å°ãæ´å½¢ããã¦åºåããã¾ããã
JSON ãã¡ã¤ã«ã¯ãåºæ¬çã«ããã¼ã¨å¤ã®ãªãã¸ã§ã¯ãï¼ããã·ã¥ã¨ãè¨ãï¼ã®å½¢å¼ã¨ãé
åã®å½¢å¼ããããã¾ããã
ãã® sample1.json ã¯ãæä¸ä½é層ããªãã¸ã§ã¯ãå½¢å¼ã«ãªã£ã¦ãã¾ãã
ãªãã¸ã§ã¯ãã®ãã¼ãå
¨ã¦è¡¨ç¤ºããæ¹æ³ã§ããé
åã«ãªã£ã¦åºåããã¾ããã
$ cat sample1.json | jq keys
[
"ka0",
"kb0"
]
å
é ã®ãã¼ã ããé¸æãããã®å¾ããã®ä¸ã®ãã¼ãé¸æãã¦ã¿ã¾ããã.ãã«ç¶ãã¦ããã¼ãç¶ãã¾ããå¤ãåãåºããã¨ãåºæ¥ã¦ãã¾ãã
$ cat sample1.json | jq .ka0
{
"ka1": "va1",
"kb1": "vb1"
}
$ cat sample1.json | jq .ka0.ka1
"va1"
ç¶ãã¦ãé
åã«ã¤ãã¦æ±ãæ¹ã§ããã¾ãã2çªç®ã®ãã¼ãæå®ãã¦é
åãåãåºãããã®å¾ãé
åã®ä¸èº«ãåãåºãã¾ããããã«ãé
åã®è¦ç´ ã®2çªç®ãæå®ãã¦ã¿ã¾ãã
$ cat sample1.json | jq .kb0
[
{
"kc1": "vc1"
},
{
"kd1": "vd1"
}
]
$ cat sample1.json | jq .kb0[]
{
"kc1": "vc1"
}
{
"kd1": "vd1"
}
$ cat sample1.json | jq .kb0[1]
{
"kd1": "vd1"
}
åºæ¬çãªæ±ãæ¹ã¯ä»¥ä¸ã«ãªãã¾ãã
jqã§è¤éãªãã¼ã¿ãæ±ã
ã§ã¯ãe-Stat ã®ãã¼ã¿ãæ±ã£ã¦ããã¾ãã
ã¾ãã¯ããªãã¸ã§ã¯ãå½¢å¼ã¨æ³å®ãã¦ããã¼ä¸è¦§ãåºãã¾ãããã¼ã1ã¤ã ã£ãã®ã§ã次ã®é層ã®ãã¼ä¸è¦§ãåºãã¾ãã
ããã«ãGUI ãã¼ã«ã§è¦ã¦ "STATISTICAL_DATA" ãä¸çªå¤§ãããã¼ã¿ã¨åãã£ã¦ããã®ã§ãããã®ãã¼ä¸è¦§ãåºãã¾ãã
GUI ãã¼ã«ã¨è¦ãæ¹ãå¤ããã¨ãããããã®ã§ãã½ã¼ãããªããã¼ä¸è¦§ã使ã£ã¦ããã¾ãã
$ cat getStatsData.json | jq keys
[
"GET_STATS_DATA"
]
$ cat getStatsData.json | jq .GET_STATS_DATA | jq keys_unsorted
[
"RESULT",
"PARAMETER",
"STATISTICAL_DATA"
]
$ cat getStatsData.json | jq .GET_STATS_DATA | jq .STATISTICAL_DATA | jq keys_unsorted
[
"RESULT_INF",
"TABLE_INF",
"CLASS_INF",
"DATA_INF"
]
大ããé
åï¼CLASSï¼ãè¦ã¦ã¿ã¾ããé
åããªãã¸ã§ã¯ãã®åæ°ã調ã¹ãã«ã¯ãlength ã使ãã¾ãã
$ cat getStatsData.json | jq .GET_STATS_DATA | jq .STATISTICAL_DATA | jq .CLASS_INF.CLASS_OBJ[2] | jq .CLASS | jq length
4086
ãã1ã¤ã®å¤§ããé
åï¼DATAï¼ãè¦ã¦ã¿ã¾ãã
$ cat getStatsData.json | jq .GET_STATS_DATA | jq .STATISTICAL_DATA | jq .DATA_INF.VALUE | jq length
12258
CLASS ã®æ¹ã®ãã¼ã¿ã®å
é ã®3ã¤ã§ãã
$ cat getStatsData.json | jq .GET_STATS_DATA | jq .STATISTICAL_DATA | jq .CLASS_INF.CLASS_OBJ[2] | jq .CLASS[:3]
[
{
"@code": "00000",
"@name": "å
¨å½",
"@level": "1"
},
{
"@code": "01000",
"@name": "åæµ·é",
"@level": "2",
"@parentCode": "00000"
},
{
"@code": "01100",
"@name": "æå¹å¸",
"@level": "4",
"@parentCode": "01000"
}
]
jqã®çµã¿è¾¼ã¿é¢æ°ã使ã
map(f) é¢æ°ã使ã£ã¦ã¿ã¾ããf ã¯ä»»æã®ãã£ã«ã¿ã§ããå
¥åã®ãªãã¸ã§ã¯ããã¾ãã¯ãé
åã«å¯¾ãã¦ãã¾ã¨ãã¦ãã£ã«ã¿ãé©ç¨ãããã¨ãåºæ¥ã¾ãã
@name
ã ãæãåºãã¦ãè¦ã¦ã¿ã¾ããæå¾ã®ã.CLASSãã®åºåã¯ã4086åã®é
åã§ãã
$ cat getStatsData.json | jq .GET_STATS_DATA | jq .STATISTICAL_DATA | jq .CLASS_INF.CLASS_OBJ[2] | jq .CLASS | jq 'map(."@name")'
[
"å
¨å½",
"åæµ·é",
"æå¹å¸",
"æå¹å¸ä¸å¤®åº",
"æå¹å¸ååº",
"æå¹å¸æ±åº",
"æå¹å¸ç½ç³åº",
"æå¹å¸è±å¹³åº",
"æå¹å¸ååº",
"æå¹å¸è¥¿åº",
"æå¹å¸åå¥åº",
"æå¹å¸æ稲åº",
"æå¹å¸æ¸
ç°åº",
"å½é¤¨å¸",
ï¼çç¥ï¼
次ã¯ãuniqe é¢æ°ã使ã£ã¦ã¿ã¾ãã
DATA ã®æ¹ã®å
é 3ã¤ã§ãã
$ cat getStatsData.json | jq .GET_STATS_DATA | jq .STATISTICAL_DATA | jq .DATA_INF.VALUE[:3]
[
{
"@tab": "2020_01",
"@cat01": "0",
"@area": "00000",
"@time": "2020000000",
"@unit": "人",
"$": "126146099"
},
{
"@tab": "2020_01",
"@cat01": "0",
"@area": "01000",
"@time": "2020000000",
"@unit": "人",
"$": "5224614"
},
{
"@tab": "2020_01",
"@cat01": "0",
"@area": "01100",
"@time": "2020000000",
"@unit": "人",
"$": "1973395"
}
]
"@tab" ãå
¨ã¦åããªã®ãã確èªãããã¨ããªã©ã«ãunique é¢æ°ã使ãã¾ãã
$ cat getStatsData.json | jq .GET_STATS_DATA | jq .STATISTICAL_DATA | jq .DATA_INF.VALUE | jq 'map(."@tab")' | jq unique
[
"2020_01"
]
å
¨ã¦åãå¤ã ã£ãããã§ãã
次ã¯ãselect é¢æ°ã使ã£ã¦ã¿ã¾ããæ¡ä»¶ã«ãããããé
ç®ã ãã表示ããã¾ãã
人å£ã1å以ä¸ã®ãã®ãæ±ãã¾ãã
$ cat getStatsData.json | jq .GET_STATS_DATA | jq .STATISTICAL_DATA | jq .DATA_INF.VALUE | jq 'map(."$")' | jq .[] | jq 'tonumber' | jq 'select(. > 100000000)'
jq: error (at <stdin>:725): Invalid numeric literal at EOF at line 1, column 1 (while parsing '-')
jq: error (at <stdin>:4811): Invalid numeric literal at EOF at line 1, column 1 (while parsing '-')
jq: error (at <stdin>:8897): Invalid numeric literal at EOF at line 1, column 1 (while parsing '-')
126146099
ããã¤ãã¨ã©ã¼ãåºã¦ãã¾ãã調ã¹ã¦ã¿ãã¨ã人å£ã®ã¨ããã -
ã«ãªã£ã¦ãã¨ããã3ãæããã¾ãããã¡ãã£ã¨å¼·å¼ã§ããã-
ãåãé¤ãã¦ããã¾ãã
$ cat getStatsData.json | jq .GET_STATS_DATA | jq .STATISTICAL_DATA | jq .DATA_INF.VALUE | jq 'map(."$")' | jq .[] | jq 'select(. != "-")' | jq 'tonumber' | jq 'select(. > 100000000)'
126146099
1ã¤ãããªãã£ãã®ã§ã1åä¸äººä»¥ä¸ã«å¤æ´ãã¾ãã
$ cat getStatsData.json | jq .GET_STATS_DATA | jq .STATISTICAL_DATA | jq .DATA_INF.VALUE | jq 'map(."$")' | jq .[] | jq 'select(. != "-")' | jq 'tonumber' | jq 'select(. > 10000000)'
126146099
14047594
61349581
64796518
4ã¤ã«ãªãã¾ããã
ãããã«
ä»åã¯ãCUI 㧠JSON ãã¡ã¤ã«ãæ±ãæ¹æ³ã¨ãã¦ããjqãã³ãã³ããç´¹ä»ãã¾ããã
ã¡ãã£ã¨é£ããã³ãã³ããªã®ã§ãããå°ã使ãè¾¼ãã§ããããã¨æãã¾ãã
æå¾ã«ãªãã¾ããããã¨ã³ã¸ãã¢ã°ã«ã¼ãã®ã©ã³ãã³ã°ã«åå ä¸ã§ãã
æ°æ¥½ã«ãããã¨ãããããé¡ããããã¾ãð
ä»åã¯ä»¥ä¸ã§ãï¼
æå¾ã¾ã§ãèªã¿ããã ãããããã¨ããããã¾ããã