jqï¼https://stedolan.github.io/jq/ï¼ã®ç´¹ä»ã§ã¯ããJSONå¦çã®ã¯ã³ã©ã¤ãã¼ãä¸è¡ééãã¨ãã¦ãã¡ããã¡ã便å©ï¼ãã¨ã¢ãã¼ã«ããã®ãå®çªã§ãããã¡ããããã¯æ¬å½ã§ãããã¡ããã¡ã便å©ï¼ãã§ããããå®ã¯ jq ã¯ãã¯ã³ã©ã¤ãã¼è¨è¿°ã«ã¨ã©ã¾ããªããããªãæ¬æ ¼çãªããã°ã©ãã³ã°è¨èªã§ãã
JSONå¦çã®ããã®DSLãDomain Specific Language | é åç¹åè¨èªããªã®ã§ãæ±ç¨è¨èªã§ã¯ããã¾ãããããããæ±ç¨è¨èªãåãã¦ããè¨èªæ©è½ã®ä¸é¨ï¼é¢æ°å®ç¾©ãã¢ã¸ã¥ã¼ã«ã·ã¹ãã ãªã©ï¼ã jq ãæã£ã¦ãã¾ããã¾ã jq ã¯ãç¬ç¹ã§æ¥½ããããã°ã©ãã³ã°ã»ãã©ãã¤ã -- âãã¤ãã©ã¤ã³æåâã«åºã¥ãã¦è¨è¨ããã¦ãã¾ãã
ãã®è¨äºã§ã¯ãã¯ã³ã©ã¤ãã¼ãè¶ ãã jq ã®ä½¿ãæ¹ã¨ãããã°ã©ãã³ã°è¨èªã¨ãã¦ã® jq ã®ç¹å¾´ãç´¹ä»ãã¾ããé·ãè¨äºã«ãªã£ã¦ãã¾ã£ãã®ã§ãä¸æ°ã«èªããã¨ããã«ä½åãã«åãã¦èªãã¨ããããç¥ãã¾ããã
å 容ï¼
- æºå
- ã³ã³ã½ã¼ã«ã«é¢ãã注æ
- ç°¡åãªä¾
- ã¢ã¸ã¥ã¼ã«ã·ã¹ãã
- ~/.jq ã«é¢ãã注æ
- ãã¤ãã©ã¤ã³æåããã°ã©ãã³ã°
- JSONãã¼ã¿ã¨ã¯ä½ã
- jq ã®å ¥åã¨ãªããã¼ã¿
- æ¡å¼µJSON Linesãã©ã¼ãããã¨JSONã·ã¼ã±ã³ã¹
- ãã¤ãã©ã¤ã³ã®åå²ã¨åæµ
- ãããã¨ãã¹å¼
- é¢æ°ã¨ãã®ãããã¡ã¤ã«
- ãµã³ãã«
- ãããã«
æºå
https://stedolan.github.io/jq/download/ ããããã©ãããã©ã¼ã ãã¨ã®å®è¡å¯è½ãã¤ããªãã½ã¼ã¹ã³ã¼ãã®tarã¢ã¼ã«ã¤ããå ¥æã§ãã¾ããå®è¡å¯è½ãã¤ããªããã¦ã³ãã¼ããã¾ãããã
jq ã®å®è¡å¯è½ãã¤ããªã¯åä¸ãã¡ã¤ã«ã§ãDLLï¼.dll ãã¡ã¤ã«ã .so ãã¡ã¤ã«ï¼ã¸ã®ä¾åæ§ã¯ãªãã®ã§ã好ããªå ´æã«ç½®ãã¦ãã¹ï¼ç°å¢å¤æ° PATHï¼ãéãã°ãããã ãã§ä½¿ãã¾ãã
jq ã®ããã¥ã¢ã«ã¯ https://stedolan.github.io/jq/manual/ ã§ãããã«ä½ã§ãæ¸ãã¦ããã¾ãããªã³ã©ã¤ã³ã»ãã¬ã¤ã°ã©ã¦ã³ã https://jqplay.org/ ããã£ã¦ãã¨ã¦ã便å©ã§ãããã¹ã¹ã¡ã
VSCodeã®æ¡å¼µæ©è½ã3ã¤ã¿ã¤ããã¾ãã
- vscode-jq : https://marketplace.visualstudio.com/items?itemName=dandric.vscode-jq
- jq Syntax Highlighting : https://marketplace.visualstudio.com/items?itemName=jq-syntax-highlighting.jq-syntax-highlighting
- Visual Code jq playground : https://marketplace.visualstudio.com/items?itemName=davidnussio.vscode-jq-playground
3ã¤ã¨ãå ¥ãã¦ãå¹²æ¸ã¨ãã¯ãªãããã§ãã
- vscode-jq : JSONãã¡ã¤ã«ãç·¨éä¸ã« jq ã«å¦çããããã¨ãã«ä¾¿å©ã
- jq Syntax Highlighting : æ¡å¼µå .jq ã®ãã¡ã¤ã«ã jq ã½ã¼ã¹ã³ã¼ãã¨ãã¦æ§æãã¤ã©ã¤ã表示ãã¦ããã¾ãã
- Visual Code jq playground : ãªã³ã©ã¤ã³ã»ãã¬ã¤ã°ã©ã¦ã³ãã¨åæ§ãªæ©è½ã VSCode å ã§å®ç¾ãã¾ãã
Visual Code jq playground ã¯ãèªåã§ä½¿ã jq ããã¦ã³ãã¼ããã¾ããåã®ç°å¢ã ã¨ã
C:\Users\m-hiyama\AppData\Roaming\Code\User\globalStorage\davidnussio.vscode-jq-playground\
ã« jq.exe ããã¦ã³ãã¼ãããã¾ãããçå±ã®ä¸ã§ã¯ãã·ã§ã«ãã使ã£ã¦ãã jq ã¨ãã¼ã¸ã§ã³ãéã£ã¦ãã¾ããªã¹ã¯ãããã¾ãããç¾å®çã«ã¯åé¡ãèµ·ãããã«ãªãã®ã§æ°ã«ããªãã¦ããã§ãããã
ã³ã³ã½ã¼ã«ã«é¢ãã注æ
æã¯ã¿ã¼ããã«ã¨ã³ã³ã½ã¼ã«ã¯å¥ãªã¢ãã§ããããä»ã©ããã¿ã¼ããã«ã¨ã³ã³ã½ã¼ã«ãåºå¥ããå¿ ç¶æ§ã¯ãªãã®ã§ããã¿ã¼ããã«ãã¨ãã³ã³ã½ã¼ã«ãã¯å義èªã¨ãã¦ä½¿ãã¾ãã
jq ã¯ãã·ã§ã«ã®ã³ãã³ãã©ã¤ã³ããèµ·åããã³ã³ã½ã¼ã«ã¢ããªã±ã¼ã·ã§ã³ã§ããå ¥åã¨åºåã¯OSã®æ¨æºå ¥åï¼æ¨æºåºåãstdin / stdoutãã使ãã¾ããã©ã®ãã©ãããã©ã¼ã ã§ã jq ã使ãã¾ãããOSï¼ã·ã§ã«ã®å½±é¿ãã¾ã£ããåããªãããã§ã¯ããã¾ããã
åã¯é常ãWindows Powershell ãã·ã§ã«ã«ä½¿ã£ã¦ã¾ãããæ¨æºå ¥åºåã«é¢ãã¦ã¯ããã£ãããã©ãã«ãèµ·ãã¾ã*1ããã®åå ã¯ï¼
- æåã¨ã³ã³ã¼ãã£ã³ã°æ¹å¼
- å é ã®BOMãByte Order Markãæå
- æ¹è¡æåï¼LF ã CR+LF ãï¼
- ANSIã¨ã¹ã±ã¼ãã·ã¼ã±ã³ã¹
- 使ç¨ãã¦ãããã©ã³ã
- ç¨ã«ããã¡ã¤ã«ã®æ«å°¾ãEnd Of Fileãã®æ¹è¡ï¼ããããªããï¼
ãããã¯ãã·ã§ã«ã»ããã°ã©ã ã ãã§ãªããã·ã§ã«ã使ç¨ãã¦ããã³ã³ã½ã¼ã«ã¦ã£ã³ãã¦ã®ä»æ§ã¨ãé¢ä¿ãã¾ããä¾ãã°ãåç¬ã®ã³ã³ã½ã¼ã«ã¦ã£ã³ãã¦å ã® PowerShell ã¨ãWindows Terminal ã®ã¿ãå ã® PowerShell ã§ã¯æåãéãã¾ããã·ã§ã«ã«ãã£ã¦ãã³ãã³ãã©ã¤ã³ã®ã¯ã©ã¼ãã£ã³ã°ãç¹æ®æåã¨ã¹ã±ã¼ãã®æ§æãéãã®ãã¹ãã¬ã¹ã§ãã
ããã§ã¯ãGit for Windows ã«ä»å±ã® bashï¼sh.exeï¼ ãã·ã§ã«ã«ä½¿ããã¨ã«ãã¾ãï¼ãMingw-w64/MSYS2 ãå ¥ããªãã¦ã Git for Windows ã§éã«åãã¿ãããåç §ï¼ï¼ åã®ã¯ã¡ãã£ã¨å¤ãã§ããç¾æç¹ï¼2022å¹´12æï¼ã§ã®ææ°ç㯠Git for Windows Version 2.38.1 ã§ãã
> git --version git version 2.35.1.windows.2 > which git C:\Program Files\Git\cmd\git.exe > which sh C:\Program Files\Git\usr\bin\sh.exe > sh --version GNU bash, version 4.4.23(2)-release (x86_64-pc-msys) Copyright (C) 2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or laterThis is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
ãã¼ãã¼ããã ^Dï¼CTRL+Dï¼ã^Zï¼CTRL+Zï¼ãæã£ãã¨ãã®æåã¯ãç°å¢ã«ããéãã¾ããWindows ã§ã¯ä¼çµ±çã«ãEOTãEnd-of-Transmission | ä¼éçµäºãã« ^D ã§ã¯ãªã㦠^Z ã使ãã¾ã*2ã^D ã¯ç¡è¦ããã¾ããPowerShell ã¯ãã®ãããªæåããã¾ãã
Unixç³»OS㧠^Z ãæã¤ã¨ãSIGTSTPã·ã°ãã«ãçºçãã¦ç¾å¨ã®ã¸ã§ããããã¯ã°ã©ã¦ã³ãã«åãã¾ãã^D ã¯EOTãEnd-of-Transmissionãã§ãããã¿ã¼ããã«ã½ããã¦ã§ã¢ã横åããã¦ãã¾ãå¯è½æ§ãããã¾ãã
Git for Windows ã® sh.exe ã¯ãLinux ã® GNU bash ãã·ãã¥ã¬ã¼ããã¦ããã®ã§ã^Z ã¯ã¸ã§ãå¶å¾¡ã§ã*3ããã^D ã¯EOTã«ãªããªãããã§ããcat - > myfile.txt
ã®ãããªå ´åã®ãã¼ãã¼ãå
¥åãçµäºãEnd-of-Transmissionãããã®ãé£ããã§ãããã¿ã¼ããã«ï¼ã³ã³ã½ã¼ã«ã使ãã®ã¯æå¤ã«å¤§å¤*4ã
ç°¡åãªä¾
æ¤ç´¢ãã¼ã¯ã¼ããjq JSONãã¨ãã§Googleæ¤ç´¢ããã¨ãjq ã®ã¯ã³ã©ã¤ãã¼ã§JSONå¦çãããä¾ãåºã¦ããã§ãããããªã®ã§ããã®ãã®è©±ã¯ããã§ã¯ãã¾ããã
jq ã¯ãsed ã AWK ãç½®ãæããç®çãæã¤ã®ã§ãããã¹ãå¦çãã§ãã¾ããã¾ããæ°å¤è¨ç®ã«ã使ãã¾ãã
"Hello, World."
ã¨ããJSONæååãããã«ã³ããããªãªãã空ç½ï¼çªå· 0x20 ã®æåï¼ãåãé¤ãã¦å¤§æååããã«ã¯ã次ã®jqã³ã¼ãã使ãã¾ããä»ã¯ãjq ã®æ§æã¯æ°ã«ããªãã¦ããã§ãããgsub
㯠global substitution ã§ãæ£è¦è¡¨ç¾[,. ]
ã«ãããããé¨åæååã空æååã§ç½®æãã¾ããascii_upcase
ã¯ãå°æåâ大æåãå¤æã§ãã
gsub("[,. ]"; "") | ascii_upcase
jqã³ã¼ããã·ã§ã«ã®ã³ãã³ãã©ã¤ã³ããå®è¡ããã«ã¯æ¬¡ã®ããã«ãã¾ãã
$ echo '"Hello, World."' | jq 'gsub("[,. ]"; "") | ascii_upcase' "HELLOWORLD"
å
¥åãã¼ã¿ã§ããJSONæåå "Hello, World."
ã¨å¦çãè¨è¿°ããjqã³ã¼ã gsub("[,. ]"; "") | ascii_upcase
ã¯ãã·ã³ã°ã«ã¯ã©ã¼ãã¼ã·ã§ã³ãã¼ã¯ã§ã¯ã©ã¼ãããå¿
è¦ãããã¾ããã·ã§ã«ã®ãã¤ãè¨å·ã¨jqã³ã¼ãå
ã®ãã¤ãè¨å·ãç´ããããã®ã§æ³¨æãã¦ãã ããã
ãã¡ããããã¡ã¤ã«ããå ¥åãã¦ããã¾ãã¾ããããã¡ã¤ã«åã¯ã2çªç®ã®ã³ãã³ãã©ã¤ã³å¼æ°ã¨ãã¦æ¸¡ãã¾ãã
$ echo '"Hello, World."'> hello.json $ cat hello.json "Hello, World." $ jq 'gsub("[,. ]"; "") | ascii_upcase' hello.json "HELLOWORLD"
å
¥åã¯ãããã«ã¯ã©ã¼ãããæååã§ãªãã¨ã¨ã©ã¼ãã¾ãããã³ãã³ãã©ã¤ã³ãªãã·ã§ã³ -R
ãä»ããã¨ãããã«ã¯ã©ã¼ããä¸è¦ã«ãªãã¾ãã
$ echo 'Hello, World.'> hello-1.json $ cat hello-1.json Hello, World. $ jq -R 'gsub("[,. ]"; "") | ascii_upcase' hello-1.json "HELLOWORLD"
-R
ãæå®ããã¨ãå
¥åãåãªãããã¹ãè¡ã ã¨è§£éãã¾ããå
é¨ã§JSONæååã«å¤æããã®ã§ãJSONå¦çç¨ã®ããã°ã©ã ããã®ã¾ã¾ããã¹ãå¦çã«ä½¿ãã¾ãã
åºåã®ããã«ã¯ã©ã¼ãã¼ã·ã§ã³ãåãé¤ãããã¨ãã¯ã-r
ãªãã·ã§ã³ãä»ãã¾ãã
$ jq -R -r 'gsub("[,. ]"; "") | ascii_upcase' hello-1.json HELLOWORLD
jq ã¯æ®éã«æ°å¤è¨ç®ãã§ãã¾ãã
$ jq '2 + 3*5'
ããããjq ã¯æ¨æºå ¥åï¼ãã®å ´åã¯ãã¼ãã¼ãï¼ããã®å ¥åãå¾ ã£ã¦æ¢ã¾ã£ã¦ãã¾ãã¾ããããã¼ã®ãã¼ã¿ãéã£ã¦ããã¾ãããã
$ echo 0 | jq '2 + 3*5' 17
ä¸ã®ããã«ããªãã¦ããJSONãã«ãnull
ããå
¥åã«ãããªãã·ã§ã³ -n
ãããã¾ãã
$ jq -n '2 + 3*5' 17
0 ãã Ï/2 ã¾ã§ã®åºéã 0.1 å»ã¿ã«åãã¦ãåç¹ã§ã®ãµã¤ã³é¢æ°ï¼ä¸è§é¢æ° sin
ï¼ã®å¤ãæ±ããã«ã¯æ¬¡ã®ããã«ãã¾ãã
$ jq -n 'range(0; 0.5 * 3.1416; 0.1) | sin' 0 0.09983341664682815 0.19866933079506122 0.2955202066613396 0.3894183423086505 0.479425538604203 0.5646424733950354 0.644217687237691 0.7173560908995227 0.7833269096274833 0.8414709848078964 0.8912073600614353 0.9320390859672263 0.963558185417193 0.9854497299884603 0.9974949866040544
ããã§ãÏ/2 ã®è¿ä¼¼å¤ãæã§å
¥ãã¾ãããjq ã§ã¯ãåå¨çã®å®æ° PI ãå®ç¾©ããã¦ãªãããã§ãï¼ãã¶ãï¼ãããéä¸è§é¢æ°asin
ã®å¤ã¨ãã¦æ±ã¾ãã¾ãã
$ jq -n '1 | asin | . * 2' 3.141592653589793
åå¨çãå¾ã 使ããããªãã次ã®ããã«é¢æ°å®ç¾©ï¼å¾è¿°ï¼ãã¾ãã
def PI: 1 | asin | . * 2 ;
é¢æ°å®ç¾©ãå«ãã 次ã®ãããªjqã¹ã¯ãªãããã¡ã¤ã« sin-values.jq ãæºåãã¾ãããã
# sin-values.jq def PI: 1 | asin | . * 2 ; range(0; 0.5 * PI; 0.1) | sin
jqã¹ã¯ãªãããã¡ã¤ã«ã¯ã-f
ã§æå®ãã¾ãã
$ cat ./sin-values.jq # sin-values.jq def PI: 1 | asin | . * 2 ; range(0; 0.5 * PI; 0.1) | sin $ jq -n -f ./sin-values.jq 0 0.09983341664682815 0.19866933079506122 0.2955202066613396 0.3894183423086505 0.479425538604203 0.5646424733950354 0.644217687237691 0.7173560908995227 0.7833269096274833 0.8414709848078964 0.8912073600614353 0.9320390859672263 0.963558185417193 0.9854497299884603 0.9974949866040544
åå¨ç PI ã®å®ç¾©ãã¹ã¯ãªãããã¡ã¤ã«ã«åãè¾¼ãã®ã§ã¯ãªãã¦ãã©ã¤ãã©ãªã¨ãã¦å ±æãããã¨ãã¯ã¢ã¸ã¥ã¼ã«ã使ãã¾ãï¼æ¬¡ç¯ï¼ã
ã¢ã¸ã¥ã¼ã«ã·ã¹ãã
jqããã°ã©ã ã³ã¼ãããã³ãã³ãã©ã¤ã³å
ã«æ¸ãã«ã¯é·ããªãéããããjqã¹ã¯ãªãããã¡ã¤ã«ãä½ã£ã¦ãããã -f
ãªãã·ã§ã³ã§æå®ãã¾ããããã°ã©ã ãã1åã®ãã¡ã¤ã«ã«åã¾ããªãã»ã©å¤§ãããªã£ããã©ããã¾ãããï¼ ãããã¯ãè¤æ°ã®ã¹ã¯ãªãããã¡ã¤ã«ã§å
±éã«ä½¿ãããé¢æ°å®ç¾©ã1ãã¡ã¤ã«ã«ã¾ã¨ãããã¨ãã¯ã©ããã¾ãããï¼ ãããã¢ã¸ã¥ã¼ã«ã·ã¹ãã ã®åºçªã§ãã
jqã¢ã¸ã¥ã¼ã«ã¨ã¯ãæ¡å¼µåã .jq ã§jqããã°ã©ã ãåãããã¡ã¤ã«ã§ããã¹ã¯ãªãããã¡ã¤ã«ã¨ã»ã¼åãã§ãããã¢ã¸ã¥ã¼ã«ã«ã¯é¢æ°å®ç¾©ããæ¸ãã¾ãããã¢ã¸ã¥ã¼ã«å ã«ããããã¬ãã«ã®å®è¡ã³ã¼ããããã¨ã¨ã©ã¼ã«ãªãã¾ãã
å®éã«ä½¿ã£ã¦ã¿ãã¨ãã¢ã¸ã¥ã¼ã«ã³ã¼ãã«ã¯å®è¡ã³ã¼ããæ¸ããªãã®ã¯ãã£ããä¸ä¾¿ã§ããã²ã¨ã¤ã®ãã¡ã¤ã«ãã¹ã¯ãªããã¨ãã¦ãã¢ã¸ã¥ã¼ã«ã¨ãã¦ã使ããããã¨ãããã®ã§ããã¢ã¸ã¥ã¼ã«ã¨ãã¦ãã¼ããããã¨ãã¯ããããã¬ãã«ã®å®è¡ã³ã¼ãã¯ç¡è¦ããã¨ããä»æ§ã§ãããã£ãæ°ããã¾ãã
[/è£è¶³]
åç¯ã®åå¨ç PI ã®å®ç¾©ãã¢ã¸ã¥ã¼ã«ãã¡ã¤ã« util.jq ã«å
¥ããã¨ãã¾ããã¢ã¸ã¥ã¼ã«ãå©ç¨ããå´ã¯æ¬¡ã®ãããªã¤ã³ãã¼ãæï¼import
ï¼ãæ¸ãã¾ãã
# sin-values.jq # import ã使ãããã«å¤æ´ import "util" as u; range(0; 0.5 * u::PI; 0.1) | sin
ã¤ã³ãã¼ãæã§æå®ããã¢ã¸ã¥ã¼ã«åã¯ãã¢ã¸ã¥ã¼ã«ãã¡ã¤ã«åããæ¡å¼µå .jq ãåãé¤ããååã§ããas
ã®å¾ã«ãããã°ã©ã å
ã§ä½¿ãæ¥é è¾åãæå®ãã¾ãããã®ååã¯ã¢ã¸ã¥ã¼ã«åã¨éã£ã¦ããã¾ãã¾ããã
ã¢ã¸ã¥ã¼ã«ã§å®ç¾©ããã¦ããé¢æ°ãå¼ã¶ã«ã¯ãæ¥é è¾::åå
ã®æ§æã使ãã¾ããu::PI
ããã®ä¾ã§ãã
ã¢ã¸ã¥ã¼ã«ãã¡ã¤ã«ã¯ã©ãã«ç½®ãã¦ããã¾ãã¾ãããä¾ãã° ~/jqLib/ ã«ç½®ããã¨ãã¾ããã¢ã¸ã¥ã¼ã«ãã¡ã¤ã«ãç½®ãããã£ã¬ã¯ããªï¼jq ã©ã¤ãã©ãªã»ãã£ã¬ã¯ããªï¼ã¯ãã³ãã³ãã©ã¤ã³ã»ãªãã·ã§ã³ -L
ã§æå®ãã¾ãã
$ cat ./sin-values.jq # sin-values.jq # import ã使ãããã«å¤æ´ import "util" as u; range(0; 0.5 * u::PI; 0.1) | sin $ jq -n -L ~/jqLib -f ./sin-values.jq 0 0.09983341664682815 0.19866933079506122 0.2955202066613396 0.3894183423086505 0.479425538604203 0.5646424733950354 0.644217687237691 0.7173560908995227 0.7833269096274833 0.8414709848078964 0.8912073600614353 0.9320390859672263 0.963558185417193 0.9854497299884603 0.9974949866040544
æ¯å -L
ãªãã·ã§ã³ã§æå®ããã®ã¯ããã©ãããã§ãããJQ_LIBRARY_PATH ã®ãããªç°å¢å¤æ°ã¯ãªãããã§ããããããã©ã«ãã®ã©ã¤ãã©ãªã»ãã£ã¬ã¯ããªã¯æ±ºã¾ã£ã¦ãã¾ãã次ã®é ã§ãã£ã¬ã¯ããªããã©ã£ã¦ã¢ã¸ã¥ã¼ã«ãæ¤ç´¢ãã¾ãã
- ~/.jq/
- $ORIGIN/../lib/jq/
- $ORIGIN/../lib/
ãã㧠$ORIGIN/ ã¯ãjq ã®å®è¡å¯è½ãã¡ã¤ã«ãç½®ããã¦ãããã£ã¬ã¯ããªã§ãããã£ã¬ã¯ã㪠~/.jq/ ã«ã¢ã¸ã¥ã¼ã«ãã¡ã¤ã«ãç½®ãã®ãä¸çªãæ軽ã§ãã
~/.jq ã«é¢ãã注æ
ã¡ãã£ã¨åããã«ããä»æ§ãªãã§ããã~/.jq ããã¡ã¤ã«ã®ã¨ãã¨ãã£ã¬ã¯ããªã®ã¨ãã§æ±ããå¤ããã¾ãã
- ~/.jq ããã¡ã¤ã«ã®ã¨ã ï¼ jq ãå®è¡ãããã¨ãã¯å¸¸ã«ã~/.jq ãã¡ã¤ã«ãã¹ã¯ãªãããã¡ã¤ã«ã¨ãã¦èªã¿è¾¼ãããã®å¾ã§ãã³ãã³ãã©ã¤ã³åãè¾¼ã¿ãã¾ãã¯
-f
ã§æå®ãããã¹ã¯ãªãããã¡ã¤ã«ã解éå®è¡ããã - ~/.jq ããã£ã¬ã¯ããªã®ã¨ã ï¼ ~/.jq/ ãããã©ã«ãã®ã¢ã¸ã¥ã¼ã«ã»ãã£ã¬ã¯ããªã¨ãã¦ä½¿ãã
~/.jq ããã¡ã¤ã«ã®ã¨ãã¯ãrcãã¡ã¤ã«ï¼åæè¨å®ãããã¹ã¯ãªãããrun commands ããï¼ã¨ãã¦æ±ãããã§ããåãååã«ããªãã§å¥ãªååã«ãã¦æ¬²ããã£ããä¾ãã°ï¼
- ~/.jqrc ãã¡ã¤ã« ï¼ jq ã®rcãã¡ã¤ã«
- ~/.jqlib/ ãã£ã¬ã¯ããª ï¼ jq ã®ããã©ã«ãã®ã©ã¤ãã©ãªã»ãã£ã¬ã¯ããª
æ³åããã«ï¼äºå®ã¯ç¥ãã¾ããï¼ãæ´å²çäºæ ã§ããããã¢ã¸ã¥ã¼ã«ã·ã¹ãã ãã§ããåã¯ã~/.jq ãã¡ã¤ã«ã«å ±éã«ä½¿ãé¢æ°å®ç¾©ãæ¸ãã¦ããããåãç®çãæã¤ã©ã¤ãã©ãªã»ãã£ã¬ã¯ã㪠~/.jq/ ã¸ã¨é²åããï¼ ãã ããäºææ§ãã ~/.jq ãã¡ã¤ã«ãæ®ããããããã¶ããã§ããã©ãã
ããã§ã¯ãrcãã¡ã¤ã«ã¨ãã¦ã® ~/.jq ã¯ä½¿ããã«ãã©ã¤ãã©ãªã»ãã£ã¬ã¯ã㪠~/.jq/ ã¨ãã¦ã¢ã¸ã¥ã¼ã«ãã¡ã¤ã«ãç½®ããã¨ã«ãã¾ããä¾ãã°ãåå¨ç PI ãå®ç¾©ãã util.jq 㯠~/.jq/util.jq ã¨ããååã«ãã¾ããããã¨ã-L
ãªãã·ã§ã³ãªãã§ã¤ã³ãã¼ãã§ãã¾ãã
ãã¤ãã©ã¤ã³æåããã°ã©ãã³ã°
ãã¤ãã©ã¤ã³ã使ãããã°ã©ãã³ã°ã¨ããã¨ãã·ã§ã«ã®ã³ãã³ãã©ã¤ã³ï¼ã·ã§ã«ã¹ã¯ãªãããæãåºãã§ããããä¾ãã°ã次ã®jqã³ã¼ãï¼å ¥åã¯ä¸è¦*5ï¼ãèãã¾ãã
"Hello, World." | gsub("[,. ]"; "") | ascii_upcase
åããã¨ãããã·ã§ã«ã³ã¼ããã³ãã³ãã©ã¤ã³ãã¯æ¬¡ã®ããã§ãã
echo "Hello, World." | tr -d ',. ' | tr a-z A-Z
ããã㨠| jq | ã·ã§ã« |
---|---|---|
æååãçæ | "Hello, World." | echo "Hello, World." |
, 㨠. ã¨ç©ºç½ãåé¤ | gsub("[,. ]"; "") | tr -d ',. ' |
å°æåã大æåã«å¤æ | ascii_upcase | tr a-z A-Z |
jq ã§ã¯ãã£ã«ã¿ã¼é¢æ°ããã¤ãã§ç¹ãã§ã¾ãããã·ã§ã«ã§ã¯ããã£ã«ã¿ã¼ã³ãã³ããOSã®ããã»ã¹ã¨ãã¦èµ·åãã¦ãã¤ãã§ç¹ãã§ã¾ãããã£ã«ã¿ã¼ããã¤ãããã¤ãã©ã¤ã³ã®æ¦å¿µãçµµã«ããã¨æ¬¡ã®ããã§ããå ¥åãç¹ã«ãªãã£ãããåºåãæ¨ã¦ã¦ãã¾ãï¼ä¾ï¼ /dev/null ã¸ãªãã¤ã¬ã¯ãããï¼ãã¨ãããã¾ãã
ãã¦ããã§ãã·ã§ã«ã®ãã¤ãã©ã¤ã³ãä¸è¬åããããã°ã©ãã³ã°ææ³ï¼ããã°ã©ãã³ã°ç°å¢ã«æãã馳ãã¦ã¿ã¾ãã
ãã¤ãã©ã¤ã³ã¯1æ¬ã«éå®ãããè¤æ°ã® -- ã¨ãã«æ¥µãã¦å¤æ°ã®ãã¤ãã©ã¤ã³ãåæã«èµ°ããã¦ããã¨ãã¾ãããã¤ãã©ã¤ã³ãæµãããã¼ã¿ã¯ç´åã§ããããã£ã«ã¿ã¼ããã»ã¹ã¯ãã¹ã¦ä¸¦åå®è¡ããã¾ãããã®æ§åãçµµã«æãã°ä¾ãã°æ¬¡ã®ããã§ãã
ãã¤ãï¼çµµã§ã¯æåã¯ã¤ã¤ã¼ï¼ã¯ããã¼ã¿ã®ã³ãã¼ãè¤åãã¼ã¿ã®å解ã«ããåå²ã§ãã¾ããè¤æ°ã®ãã¼ã¿ãè¤åãã¼ã¿ï¼é åãªã©ï¼ã¨ãã¦ã¾ã¨ãããã¨ã«ããåæµã§ãã¾ãããã£ã«ã¿ã¼ã¯ã主è¦ãªå ¥å以å¤ã«ååä»ããã¤ãããã®å¯æ¬¡çå ¥åã使ããã¨ãã§ãã¾ããçµµã®ä¸çªä¸ã®ä¸è§ã®ãã£ã«ã¿ã¼ã¯å ¥åãªãã§åºåãçæãããã£ã«ã¿ã¼ã§ããçµµã«ã¯åºã¦ãã¦ã¾ãããããã£ã«ã¿ã¼ã¯ãã¤ãããã®å ¥å以å¤ã«å¼æ°ãargumentããæã£ã¦ããã¾ãã¾ããã
大è¦æ¨¡ãªãã¤ãï¼ãã£ã«ã¿ã¼ã®ãã¼ã¿å¦çãããã¯ã¼ã¯ãå®ç¾ããã«ã¯ãErlangã®ä»®æ³æ©æ¢° BEAM Virtual Machine ã®ããã«ã1ã¤ã®ã³ã³ãã¥ã¼ã¿ä¸ã§ä½åä¸ãã®ãã¤ã¯ãããã»ã¹ãåæå®è¡ã§ããç°å¢ããµããããããç¥ãã¾ããã
jq ã¯ããã¤ãï¼ãã£ã«ã¿ã¼ã®ãã¼ã¿å¦çãããã¯ã¼ã¯ãç®å°åããï¼ãããã¯ç¾å®çã«ããï¼ãã¼ã¸ã§ã³ãå®ç¾ãã¾ããjq ã®å ´åã¯ï¼
- ãã£ã«ã¿ã¼ã¯ãæ°¸ç¶çã«åãç¶ããããã§ã¯ãªãã¦ãã²ã¨ã¤ã®å¦çåä½ã®å¦çãçµããã¨æ¶ãã¦ãã¾ãã¯ã³ã·ã§ããã»ãã¤ã¯ãããã»ã¹ãã¯ã³ã·ã§ããã»ãã¤ã¯ãããã»ã¹ã¨ã¯ãã¤ã¾ãã¯é¢æ°ã
- ä½æ¬ãã®ãã¤ãã©ã¤ã³ãæ±ããããåæã«å ¥åãå¦çãèµ·ããããã§ã¯ãªããæéçã«é 次ã«ãè¤æ°ã®ãã¤ãã©ã¤ã³ãçæããã¦ç¨¼åãããåºåãæéçã«é 次ã«å®è¡ãããã
- ãã¤ãã®åå²ã¯ã§ããããåæµã¯ã§ããªããã¨ããããåæµã§ãããã©ããã¯ãè¤æ°æ¬ã®ãã¤ãã表ããã¼ã¿ï¼å¾è¿°ã®ã·ã¼ã±ã³ã¹ï¼ã®æ§è³ªã«ããã
jq ã®å¦çã¯ãçæ³çãªâãã¤ãï¼ãã£ã«ã¿ã¼ã®ãã¼ã¿å¦çãããã¯ã¼ã¯âã¨æ¯è¼ãã¦èããã¨åãããããã¨æãã¾ãã
ã¨ããã§ãåã«ã¨ã£ã¦ãã¤ãï¼ãã£ã«ã¿ã¼ã¯ãç¹å¥ãªæãå ¥ããããå¦çæ¹å¼ã§ãã
jq ãè¦ã¦ããªããã¨æã£ãã®ãããããªï¼ä¸è¨ã®åç §å ã«æ¸ããï¼äºæ ããã§ãã
JSONãã¼ã¿ã¨ã¯ä½ã
jq ã¯ããã¹ãå¦çï¼æ°å¤è¨ç®ãã§ãã¾ããã主ããå¦ç対象ã¯JSONãã¼ã¿ã§ãã
åãä¸éãããJSONãã¼ã¿ãã¨ããè¨èããã使ãã¾ããããJSONãã¼ã¿ãã¯ææ§ãªè¨èãªã®ã§ããã£ã¨ç²¾å¯ãªç¨èªãå¹¾ã¤ãå°å ¥ãã¾ãã
JSONã®ä»æ§ RFC8259 https://datatracker.ietf.org/doc/html/rfc8259 ã«ããã¨ãJSONå¤ãJSON valueãã¨ã¯æ¬¡ã®ãã®ã§ãã
A JSON value MUST be an object, array, number, or string, or one of the following three literal names:
ãfalse
ãnull
ãtrueJSONå¤ã¨ã¯ããªãã¸ã§ã¯ããé åãæ°å¤ãæååãã¾ãã¯æ¬¡ã®3ã¤ã®ãªãã©ã«åã®ã©ããã§ãªãã¦ã¯ãªããªãã
ãfalse
ãnull
ãtrue
JSONå¤ã¨ã¯ãJSONåã®ã¤ã³ã¹ã¿ã³ã¹ã§ãããJSONåã¯æ¬¡ã®ä¸ä½åã«ç´°åã§ãã¾ãã
- ãã«ãnullãå ï¼ å¤ null ã®ã¿ãããªãã·ã³ã°ã«ãã³åãåã®ååã¨å¤ã®ååãåãï¼ãªã¼ãã¼ãã¼ãï¼ãªã®ã§æ³¨æã
- ãã¼ã«ãbooleanãå ï¼ å¤ true ã¨å¤ false ãããªãåã
- æ°å¤ãnumberãå ï¼ å®è£ ä¸ã¯ IEEE754 å精度浮åå°æ°ç¹æ°åã ããæ½è±¡çãªå®æ°åã ã¨èãã¦ãããã
- é åãarrayãå ï¼ æéåã®JSONå¤ã®é åºä»ãã®ä¸¦ã³ã®åã
- ãªãã¸ã§ã¯ããobjectãå ï¼ æååãã¼ã¨JSONå¤ãããªãããã·ã¥ããããè¾æ¸ | é£æ³é åãåã
JSONå¤ã¯ãJavaScriptãã¯ããã¨ããããã°ã©ãã³ã°è¨èªãã¡ã¢ãªã¼ä¸ã§æ±ããã¼ã¿ã ã¨æã£ã¦ããã§ãããã
âJSONå¤âã¨ãâJSONãã©ã¼ãããã®ããã¹ãâ -- ã¤ã¾ãJSONããã¹ããJSON textãã¯å¥ç©ã§ããåãã RFC8259 ã«ããã¨ï¼
A JSON text is a sequence of tokens. The set of tokens includes six structural characters, strings, numbers, and three literal names.
JSONããã¹ãã¯ããã¼ã¯ã³ã®åã§ããããã¼ã¯ã³ã¯ã6ã¤ã®æ§é æåã¨ãæååãæ°å¤ã3ã¤ã®ãªãã©ã«åãããªãã
JSONå¤ãJSONããã¹ããåãããã«æãã¾ãããJSONããã¹ãã¯ãããã¾ã§æåã®ä¸¦ã³ã§ãããã¼ã¯ã³ã¯ã²ã¨ããã¾ãã®æåã®ãã¨ã§ã6ã¤ã®æ§é æåã¨ã¯ï¼
- å·¦å³ã®ãã©ã±ããæå '[' 㨠']'
- å·¦å³ã®ãã¬ã¤ã¹æå '{' 㨠'}'
- ã«ã³ãæå ','
- ã³ãã³æå ':'
3ã¤ã®ãªãã©ã«åã¨ã¯æ¬¡ã§ãã
- åå true
- åå false
- åå null
JSONããã¹ãã«ããããæååãæ°å¤ãã¯ãJSONå¤ã®æååãæ°å¤ã®ãã¨ã§ã¯ãªãã¦ããæåå表ç¾ã®ææ³è¦åãæ°å¤è¡¨ç¾ã®ææ³è¦åã«å¾ã£ãæåã®ä¸¦ã³ãã®ãã¨ã§ãã
JavaScriptã® JSON.parse
é¢æ°ã¯JSONããã¹ããåãåã£ã¦JSONå¤ãè¿ãã¾ããä¸æ¹ã® JSON.stringify
é¢æ°ã¯JSONå¤ãåãåã£ã¦JSONããã¹ããè¿ãã¾ããæ··ä¹±ããã¡ãªã®ã¯ãâæååJSONå¤âï¼åãstringåã§ããJSONå¤ï¼ã¨âæååJSONããã¹ãâï¼æåå表ç¾ã®ææ³è¦åã«å¾ã£ãæåã®ä¸¦ã³ï¼ã®åºå«ã§ãã
JSON.stringify("Hello") // => "\"Hello\"" JSON.parse("\"Hello\"") // => "Hello"
JSONå¤ "Hello"
ã¯ãã¼ã¿ã¨ãã¦5æåã®ä¸¦ã³ã§ããããJSONããã¹ãã«ããã¨ä¸¡ç«¯ã®ã¯ã©ã¼ããå
¥ãã¦7æåã¨ãªãã7æåã "\"Hello\""
ã¨æåå表ç¾ããã¨è¦ãç®11æåã«ãªãã¾ãã
jq ã®å ¥åã¨ãªããã¼ã¿
jq ã®å ¥åã¯ããã¡ã¤ã«ã¾ãã¯ãã¼ãã¼ãããã®JSONããã¹ãã§ããããããã¼ãºãæ§æ解æããã¦JSONå¤ã¨ãã¦ããã¤ãã©ã¤ã³ã§å¦çãã¦ãçµæãã¾ãJSONããã¹ãã«ã¢ã³ãã¼ãºãæååå | stringify | ã·ãªã¢ã©ã¤ãºããã¦OSã®æ¨æºåºåã«æ¸ãåºãã¾ãã
å®ã¯ãjq ãåãä»ããå ¥åã¯ãé常ã®JSONããã¹ãããä¸è¬çã§ããä¾ãã°ã1è¡ã«æ¸ããJSONããã¹ããè¤æ°è¡ã«ä¸¦ã¹ãããã¹ããã©ã¼ãããã§ããJSON Lineså½¢å¼ã®ãã¡ã¤ã«ãèªãã¾ãã次ã¯JSON Lineså½¢å¼ã®ãã¡ã¤ã«ã®ä¾ã§ãã
"Hello, World." 3 57 {"a":1, "b":[2, 3]}
ãã®ãã¡ã¤ã«å ¥åããã¼ãºããçµæã¯åä¸ã®JSONå¤ã§ã¯ãªããJSONå¤ã®é åã§ãããã¾ãããããã§ã¯ãJSONã·ã¼ã±ã³ã¹ãJSON sequenceãã¨å¼ã³ã説æã®ããã«æ¬¡ã®å½¢ã§è¡¨è¨ãããã¨ã«ãã¾ãã
ãã"Hello, World."; 3; 57; {"a":1, "b":[2, 3]}ã
JSONã·ã¼ã±ã³ã¹ã¨å¼ã°ãããã©ã¼ãããã®ä»æ§ã RFC7464 ã«ããã¾ãã
ããããã»ã¨ãã©ä½¿ããã¦ãªãããä»å¾ä½¿ãããã¨ãæãã¾ãããJSON Lines ã§äºè¶³ããããã§ãããªã®ã§ãRFC7464 ã® JSON sequence ã¯ãªãã£ããã®ã¨ãã¦æ±ãã¾ãã
ã¡ãªã¿ã«ãjq 㯠RFC7464 ã® JSON sequence å½¢å¼ã®ãã¡ã¤ã«ã--seq
ãªãã·ã§ã³ã§èªããããã§ãã
[/è£è¶³]
jq ã«ããã¦ã¯ãåä¸ã®JSONå¤ãå¦çãã¦ããã®ããJSONã·ã¼ã±ã³ã¹ãå¦çãã¦ããã®ãã®åºå«ã¯æ¥µãã¦éè¦ã§ãã常ã«æèãã¦ããå¿ è¦ãããã¾ãã
ä¾ãã°ã. + 1
ã¯å
¥åãããJSONå¤ï¼ãããï¼ã« 1 ã足ãå¦çã§ããæ°å¤ã®JSONã·ã¼ã±ã³ã¹ãå
¥åãããã¨ãã·ã¼ã±ã³ã¹é
ç®ã®åæ°å¤ã« 1 ã足ãã¾ãã
$ jq '. + 1' 3 4 5 6 10 11
ä¸ã®ä¾ã§ã3, 5, 10 ããã¼ãã¼ãããã®å ¥åã§ã 4, 6, 11 ã jq ããã®åºåã§ããé åãå ¥åã«ããã¨ã¨ã©ã¼ã«ãªãã¾ãã
$ jq '. + 1' [3, 5, 10] jq: error (at:1): array ([3,5,7]) and number (1) cannot be added
é
åã®åé
ç®ã« 1 ã足ãã«ã¯ map
é¢æ°ã使ãã¾ãã
$ jq 'map(. + 1)' [3, 5, 10] [ 4, 6, 11 ]
é
åã§ããå
¥åãã·ã¼ã±ã³ã¹ã«ã°ããã«ã¯ã[]
ãå¾ç½®ãã¾ãã
$ jq '.[] | . + 1' [3, 5, 10] 4 6 11
åºåã¯åä¸ã®é åã§ã¯ãªãã¦ã4ã¤ã®æ°å¤ãããªãã·ã¼ã±ã³ã¹ã§ããçµµã«æãã¨æ¬¡ã®ããã§ãã
å¦çã®éç¨ã¯æ¬¡ã®ããã«æ¸ããã¨ãã§ãã¾ãã
- å ¥åãããé åï¼é·ã1ã®ã·ã¼ã±ã³ã¹ï¼ï¼ ã[3, 5, 10]ã
- ããããã©ããã·ã¼ã±ã³ã¹ï¼ ã3; 5; 10ã
- ã·ã¼ã±ã³ã¹ã®åé ç®ã« 1 ã足ãã¨ï¼ ã4; 6; 11ã
æ¡å¼µJSON Linesãã©ã¼ãããã¨JSONã·ã¼ã±ã³ã¹
JSON Linesãã©ã¼ãããã§ã¯ã1è¡ã®ããã¹ãè¡ã«1ã¤ã®JSONããã¹ããæ¸ãå¿ è¦ãããã¾ããjq ã®å ¥åãã©ã¼ãããã¯ãã£ã¨èªç±ã§ã次ã®ããã«æ¸ãã¦ãã¡ããã¨ãã¼ãºãæ§æ解æããã¦ããã¾ãã
"Hello, World."3 57{ "a":1, "b": [2, 3] }
åJSONããã¹ãã¯ç©ºç½ãæ¹è¡ã§åºåãã°ããã®ã§ããå·®ãéãããªããªããåºåãæåãªãã§ããã¿ãªãã£ã¤ãã¦æ¸ãã¦ã大ä¸å¤«ã§ãããã®ãã©ã¼ããããããã¹ãæ§æããä»®ã«æ¡å¼µJSON Linesãã©ã¼ããããextended JSON Lines formatãã¨å¼ãã§ããã¾ãããã
æ¡å¼µJSON Linesãã©ã¼ãããã§ã´ã·ã£ã´ã·ã£ã«æ¸ãããå
¥åããããããªJSON Linesãã©ã¼ãããã«æ´å½¢ãã¦åºåããã«ã¯ jq -c .
ã¨ãã¾ãã-c
㯠Compact Output ãªãã·ã§ã³ã§ããããä»ããªãã¨æ¹è¡ãã¤ã³ãã³ããããããå
¥ãã¾ãï¼ããªãã£ããªã³ãï¼ã
æ¡å¼µJSON Linesãã©ã¼ãããããã®å ¥åã¯ããã¼ãºããã¦JSONã·ã¼ã±ã³ã¹ã¨ãªã£ã¦ããå¦çããã¾ããå ¥åããå¾ãããè¤æ°é ç®ã®ã·ã¼ã±ã³ã¹ãã²ã¨ã¤ã«ã¾ã¨ãããã¨ã¯ã§ãã¾ãããããããå¦çã®ä¸éã§é åããä½ãããè¤æ°é ç®ã·ã¼ã±ã³ã¹ã¯ãå¦çã®å¾ã§ã¾ã¨ãããã¨ãã§ãã¾ãã次ã¯ãã°ããã¦ã»å¦çãã¦ã»ã¾ã¨ããä¾ã§ãã
$ jq '[ .[] | . + 1 ]' [3, 5, 10] [ 4, 6, 11 ]
ã¾ã¨ããããªãã·ã¼ã±ã³ã¹ã¯æ®çºæ§ã·ã¼ã±ã³ã¹ãvolatile sequenceããã¾ã¨ãããã¨ãã§ããã·ã¼ã±ã³ã¹ã¯ä¸æ®çºæ§ã·ã¼ã±ã³ã¹ãnon-volatile sequenceãã¨å¼ã¶ãã¨ã«ãã¾ãããã
æ®çºæ§ã·ã¼ã±ã³ã¹ã§ã¯ãé ç®ã®å¦çãçµããã¨ããã®çµæã¯ã¡ã¢ãªã¼ããæ¶ãå»ãã¾ããå¾ã®å¦çã®çµæã¨çµã¿åããããã«ã失ããªã£ã¦ããããã©ãã«ããªãã¾ãããä¸æ¹ãä¸æ®çºã·ã¼ã±ã³ã¹ã§ã¯ããã¹ã¦ã®é ç®ã®å¦ççµæãã¡ã¢ãªã¼å ã«ä¿æããã¾ãããªã®ã§ããã¹ã¦ã®å¦ççµæãä¸æ¬ã®é åã«ã¾ã¨ãããã¨ãã§ãã¾ãã
ãã¤ãã©ã¤ã³ã®åå²ã¨åæµ
ãã¤ãã©ã¤ã³ gsub("[,. ]"; "") | ascii_upcase
ã§å¦çããçµæã¨ãçµæã®æååã®é·ãã¨ããã¨ã®æååã®é·ãããã®é ã§è¡¨ç¤ºãããã¨ãã¾ããããæååã®é·ããæ±ããé¢æ°ã¯length
ã§ã*6ã
ãã¤ãã©ã¤ã³ã®çµµã§èããã®ãå¾çã§ãã次ã®ãããªçµµã«ãªãã¾ãã
ãã®çµµãjqã³ã¼ãã«åãåãã¨æ¬¡ã®ããã§ããã°ã¬ã¼ã®ç®±ã丸æ¬å¼§ã«ãªãã¾ãã
( (gsub("[,. ]"; "") | ascii_upcase) | (., length) ) , length
ãããã¯ããã®æç¹ã»å ´æã§ã®å ¥åãåç §ããè¨å·ã§ãããçµµã§ãé»ãããããæãã¦ããã¾ãããã«ã³ãã使ãã¨ãåãå ¥åã«å¯¾ãã¦2ã¤ã®å¦çï¼ã«ã³ãã®å·¦å³ï¼ãããæå³ã«ãªããçµæçã«ãã¤ãã©ã¤ã³ãåå²ãã¾ãã2åã®ã«ã³ããããã®ã§ãåå²ã2åèµ·ãã¦ãã¾ãã
å
¥åã« "Hello, World."
ãä¸ããã¨ã次ã®åºåãå¾ããã¾ãã
"HELLOWORLD" 10 13
åå²ãããã¤ããã¯ã¤ã¤ã¼ãéã¯ä¸æ®çºæ§ã·ã¼ã±ã³ã¹ãªã®ã§ãé åã«ã¾ã¨ãããã¨ãã§ãã¾ããå ¨ä½ããã©ã±ããã§å²ãã¨é åã«ã¾ã¨ã¾ãã¾ãã
[ ( (gsub("[,. ]"; "") | ascii_upcase) | (., length) ) , length ]
åºåã¯æ¬¡ã®ããã§ãã
["HELLOWORLD",10,13]
ãã¤ãã©ã¤ã³ã®çµµã¨jqã³ã¼ãã®ç¸äºç¿»è¨³ã¯ããã¬ã¼ãã³ã°ãã¦æ £ãããããªãã§ããã
ãã¤ãã©ã¤ã³ãåå²ãããå¥ãªæ¹æ³ã¨ãã¦ãå¤æ°ãvariableãã使ãæ¹æ³ãããã¾ããjq ã®å¤æ°ã¯é常ã®ããã°ã©ãã³ã°è¨èªã®å¤æ°ã¨ã¯ã¾ã£ããå¥ç©ã§ã*7ãå¤æ°ã¸ã®ä»£å ¥ã«è¦ããæ§æã¯ãååä»ããã¤ããä½ã£ã¦ããã¤ãã©ã¤ã³ã®ä¸æµã®ãã£ã«ã¿ã¼éãååä»ããã¤ããåç §ã§ããããã«ãã¾ãã
次ã®çµµã§ãéãã¯ã¤ã¤ã¼ã¯ååä»ããã¤ãã表ãã¾ãã
ãããjqã³ã¼ãã«ããã¨æ¬¡ã®ããã§ãã
length as $len | [ (gsub("[,. ]"; "") | ascii_upcase) | (., length), $len ]
ããã§ãlength as $len
ã¯ã$len
ã¨ããååã®ãã¤ããä½ã£ã¦ãlength
ã®å¤ãæµãã¾ããæ¬æµã®ãã¼ã¿ã¯ä½ããããã«ãã®ã¾ã¾ä¸æµï¼çµµã®å³æ¹åï¼ã«æµãã¾ãããã¤ãã©ã¤ã³ã®å¾ç¶ãããã£ã«ã¿ã¼ã¯ãæ¬æµï¼ç¡åãã¤ãï¼ã®ãã¼ã¿ã¨ååä»ããã¤ãã®ãã¼ã¿ã®ä¸¡æ¹ã使ãã¾ããæ¬æµãããã .
ã§åç
§ããååä»ããã¤ãã¯åå $len
ã§åç
§ãã¾ãã
ä»ã®ä¾ã§ã¯ãååä»ããã¤ã$len
ã¯ãæå¾ã«ã·ã¼ã±ã³ã¹ãé
åã«ã¾ã¨ããæã§ä½¿ããã¦ãã¾ããçµµã®åè§å½¢ãjqã³ã¼ãã®ãã©ã±ããã«å¯¾å¿ãã¾ãããã¤ãã©ã¤ã³ã®é¢ããå ´æã«æ
å ±ãéãã«ã¯ååä»ããã¤ããå¤æ°ãã便å©ã§ãã
ãããã¨ãã¹å¼
ããã.
ã¯ãç¾å¨ã®å
¥åã示ãè¨å·ã§ããascii_upcase
ãlength
ã¯ãæé»ã«å
¥åãå¦çããã®ã§ããããæ¸ãå¿
è¦ã¯ããã¾ããããããã. + 1
ã¨ã(., length)
ã ã¨ããããæ示çã«æ¸ãå¿
è¦ãããã¾ãã
ããããé¢æ°ã ã¨æãã¨ãå ¥åããã®ã¾ã¾è¿ãâidé¢æ°âã§ããååããã£ãã»ããå®å¿ãããªãã次ã®é¢æ°å®ç¾©ããã¦ããã°ããã§ããããå®éãjq ã³ã¼ãä¸ã§ãããããidé¢æ°ã¨è§£éããã»ããåããããããã¨ãããã¾ãã
def id: . ;
jq ã§ã¯ãããããå¥ãªæå³ã§ã使ããã¦ãã¦ãããã¯ãããããæã§ãããããã®å¥ãªç¨éã¯ããã¹å¼ãpath expressionãã®åºåãè¨å·ã§ããâJSONãªãã¸ã§ã¯ãã®ããããã£ï¼JSONé
åã®é
ç®âã«ã¢ã¯ã»ã¹ããããã®ãã¹å¼æ§æã«ã¯ãJSONPath ã
JSON Pointer ãããã¾ããjq ã®ãã¹å¼ã¯æ¬¡ã®æ§æãæã¡ã¾ãã
- ãªãã¸ã§ã¯ãã®ããããã£ã¸ã®ã¢ã¯ã»ã¹ã¯ããããã¨ããããã£åã使ããä¾ï¼
.givenName
- é
åã®é
ç®ã¸ã®ã¢ã¯ã»ã¹ã¯ããã©ã±ããã¨é
ç®çªå·ãã¤ã³ããã¯ã¹ãã使ããä¾ï¼
[3]
- ãªãã¸ã§ã¯ãã®ããããã£ã¸ã®ã¢ã¯ã»ã¹ã«ããã©ã±ããã¨ããããã£åæååã使ã£ã¦ããããä¾ï¼
["given name"]
- ä¸è¨ã®ãããã¾ãã¯ãã©ã±ããã並ã¹ã¦ãJSONå¤ã¸ã®ã¢ã¯ã»ã¹çµè·¯ãæ¸ããä¾ï¼
.members[3].givenName
jq ã®ãã¹å¼ã«æ
£ããã«ã¯ããªã³ã©ã¤ã³ã¾ãã¯VSCodeã®ãã¬ã¤ã°ã©ã¦ã³ãã§ãpath
é¢æ°ã¨getpath
é¢æ°ã試ãã¦ã¿ãã®ãä¸çªã§ããpath
é¢æ°ã¯ãjq ãã¹å¼ããããããã£åã¾ãã¯é
ç®çªå·ãã¤ã³ããã¯ã¹ãã®é
åã«ç´ãã¦ããã¾ãã
path(.) # => [] , path(.memebers) # => ["members"] , path(.memebers[3]) # => ["members", 3] , path(.memebers[3].givenName) # => ["members", 3, "givenName"]
getpath
é¢æ°ã¯ãå¼æ°ã«æ¸¡ãããã¹å¼ã«ãããJSONå¤ã®ä¸é¨åãåãåºãã¾ãããã ããçã®ãã¹å¼ã§ã¯ãã¡ã§ãpaht
é¢æ°ã§äºåã«é
åã«ãããã¼ã¿ã渡ãã¾ã*8ãå
¥åãã¼ã¿ã§ããJSONå¤ã次ã®ããã ã¨ãã¾ãããã
{ "bandName": "The Beatles", "members": [ { "givenName": "John", "familyName": "Lennon" }, { "givenName": "Paul", "familyName": "McCartney" }, { "givenName": "George", "familyName": "Harrison" }, { "givenName": "Ringo", "familyName": "Starr" } ] }
getpath
é¢æ°ã«ããé¨åæ½åºã¯æ¬¡ã®ããã«ãªãã¾ãã
getpath(path(.members[3].givenName)) # => "Ringo"
getpath(["members",3,"givenName"]) # => "Ringo"
çã®ãã¹å¼ãæ¸ãã¦ãçµæã¯åãã§ãã
.members[3].givenName # => "Ringo"
注æãã¹ãã¯ãå é ã®ãããããç¾å¨ã®å ¥åã表ããããã¨ããã¹å¼ã®å é ã®ããããå ¼ä»»ãã¦ãããã¨ã§ãã2çªç®ä»¥éã®ãããã¯åã«ãã¹å¼ã®ä¸é¨ã§ãã
ãã¹å¼ã使ãã°é¨åæ½åºãçãæ¸ãã¾ããããã¤ãã©ã¤ã³ã«å解ãããã¨ãã§ãã¾ãã
.members | .[3] | .givenName # => "Ringo"
é¢æ°ã¨ãã®ãããã¡ã¤ã«
é¢æ°å®ç¾©ã®ä¾ã¯æ¢ã«åºã¦ãã¾ãã
def PI: 1 | asin | . * 2 ;
def id: . ;
é¢æ°ã¯ãjqã³ã¼ãã«ååãä»ãããã®ã§ããã£ã«ã¿ã¼ã¨ãã¦ä½¿ãã¾ãã
é¢æ°ã¯æé»ã®å ¥å以å¤ã«ãå¼æ°ãåããã¨ãã§ãã¾ãã
def increment(d): . + d ; (1 | increment(2)) # => 3 , (2 | increment(1)) # => 3 , (10 | increment(-1)) # => 9
å帰çé¢æ°ãæ¸ãã¾ãã次ã¯ãæ°å¤ã®é
åã®ç·åãæ±ããé¢æ°ã§ãï¼åãæ©è½ã®çµã¿è¾¼ã¿é¢æ°add
ãããã¾ãï¼ã
def total: if (length == 0) then 0 else .[0] + (.[1:] |total) end ; [1,2,3,4,5] | total # => 15
reduce
æ§æã使ãã°æ¬¡ã®ããã
def total: reduce .[] as $i (0; . + $i) ; [1,2,3,4,5] | total # => 15
å ¥åãã³ãã¼ãã¦é·ã2ã®ã·ã¼ã±ã³ã¹ã«ããé¢æ°ã¯ï¼
def dup: (., .) ; 10 | dup # => ã10; 10ã
é·ã2ã®ã·ã¼ã±ã³ã¹ã§ã¯ãªãã¦ãé·ã2ã®é åã«ããé¢æ°ãªãï¼
def dup_array: [., .] ; 10 | dup_array # => [10, 10]
jq ã¯ãå®è¡æã®åã¨ã©ã¼ã¯èµ·ããã¾ãããéçã«åãã§ãã¯ãã§ããåã·ã¹ãã ã¯æã£ã¦ãã¾ãããã¦ã¼ã¶ã¼ãåãå®ç¾©ãããã¨ãã§ãã¾ãããããããé¢æ°ã®å ¥åã»åºåã»å¼æ°ã®åãææ¡ãã¦ãããã¨ã¯éè¦ã§ããã³ã¡ã³ãå ã«ãé¢æ°ã®ãããã¡ã¤ã«ï¼åã®ä»æ§ï¼ãæ¸ãã¦ãããã¨ã«ãã¾ãã
ä»»æã®JSONå¤ã®åã any ã¨ãã¾ããIGNORE ã¯ãå ¥åãä½ã§ãã£ã¦ãç¡è¦ããããæ¨ã¦ãããããã¨ãæå³ãã¾ããåã®æ¸ãæ¹ã¯è©³ãã説æãã¾ããããæ³åã¯ä»ãã§ãããã
# IGNORE -> number def PI: 1 | asin | . * 2 ; # any -> any def id: . ; # (number) : number -> number def increment(d): . + d ; # [number*] -> number def total: if (length == 0) then 0 else .[0] + (.[1:] |total) end ; # any -> ãany; anyã def dup: (., .) ; # any -> [any, any] def dup_array: [., .] ;
ãã®è¨äºã§åºã¦ããçµã¿è¾¼ã¿é¢æ°ã®ãããã¡ã¤ã«ãæ¸ãã¦ããã¾ã*9ã
gsub(string; string) : string -> string
ascii_upcase : string -> string
null : IGNORE -> null
sin : number -> number
range(number; number; number) : IGNORE -> [number*]
asin : number -> (number | null)
map(JqCode) : [any*] -> [any*]
(.[]) : [any*] -> ãany*ã
length : (null | number | string | [any*] | object)-> number
path(PathExp) : IGNORE -> [(string | number)*]
getpath([(string | number)*]) : any -> any
ãµã³ãã«
軽éãªRDBã·ã¹ãã ã§ããSQLiteã®ãã¼ã¿ãã¼ã¹ã»ã¹ãã¼ãï¼ãã¼ãã«ã¹ãã¼ãã®éã¾ãï¼ãJSONæ§æã§è¨è¿°ãããã¨ã«ãã¾ããä¾ãã°æ¬¡ã®ããã«ã
[ { "tableName": "bib", "columns":[ { "name" : "id", "type" : "INTEGER", "notNull" : true, "primaryKey" : true }, { "name" : "Title", "type" : "TEXT", "notNull" : true }, { "name" : "Author", "type" : "TEXT", "notNull" : true }, { "name" : "Date", "type" : "TEXT", "typeComment" : "date" }, { "name" : "Pages", "type" : "TEXT" }, { "name" : "URL", "type" : "TEXT", "typeComment" : "url" }, { "name" : "inMyBlogPage", "type" : "TEXT", "typeComment" : "url" } ] } ]
ãã®ä¾ãè¦ãã°ããã¼ã¿ãã¼ã¹è¨è¿°ã®JSONæ§æã®è¦åã¯ã ãããåããã¨æãã¾ãããããã¡ãã¨ãããã¼ã¿åå®ç¾©ãæ¸ãã¦ããã¾ãã使ãã¹ãã¼ãè¨èªã¯ãKuwataããã¨åãä½ã£ã¦ä½¿ã£ã¦ããCatyã¹ãã¼ãã§ããCatyã¹ãã¼ãã¯è¦ãã°åããæ§æã§ããã以ä¸ã®è¨äºã«è§£èª¬ãããã¾ãã
次ã®Catyã¹ãã¼ãã¯ããã¼ã¿ãã¼ã¹ã»ã¹ãã¼ãã®JSONè¨è¿°ã®åãå®ç¾©ããã¹ãã¼ãã§ã*10ã
// ã«ã©ã ã®ä»æ§ãè¨è¿°ãããã¼ã¿ã®å type ColumSpec = { "name" : string, "type" : string(format = "SQL_typeName"), "typeComment" : string?, "notNull" : boolean?, "primaryKey" : boolean?, "default" : string(format="SQL_value")? }; // ãã¼ãã«ã®ä»æ§ãè¨è¿°ãããã¼ã¿ã®å type TableSpec = { "tableName" : string, "columns" : [ColumnSpec*] }; // ãã¼ã¿ãã¼ã¹ã®ä»æ§ãè¨è¿°ãããã¼ã¿ã®å type DbSepc = [TableSpec*];
JSONæ§æã§æ¸ããããã¼ã¿ãã¼ã¹ã»ã¹ãã¼ãè¨è¿°ãèªã¿è¾¼ãã§ãæå®ããããã¼ãã«ã®CREATE TABLE
æãçæããããã°ã©ã ã jq ã§æ¸ãã¦ã¿ã¾ãã以ä¸ã® jq ããã°ã©ã ã¯ã¢ã¸ã¥ã¼ã«ãªã®ã§ãã©ã¤ãã©ãªã»ãã£ã¬ã¯ããªï¼ä¾ãã° ~/.jq/ ï¼ã«ãã¡ã¤ã«ãç½®ãã¦ã次ã®ããã«å¼ã³åºãã¾ãã
$ jq -r 'import "create-table" as DB; DB::main("bib")' dbspec.json
# create-table.jq # (name: string) : DbSpec -> (TableSpec | null) def getTableSpecFromDbSpec(name): [.[] | select(.tableName == name)] | if (length == 0) then null elif (length == 1) then .[0] else error("more than oen table") end ; # ColumnSpec -> string def makeColumnItemText: [ "\(.name | ascii_downcase) \(.type)", (if (.notNull? !=null and .notNull) then "NOT NULL" else "" end), (if (.primaryKey? != null and .primaryKey) then "PRIMARY KEY" else "" end), (if (.default? !=null) then "DEFAULT " + .default else "" end) ] | join(" ") ; # [ColumnSpec*] -> string def makeColumnListText: [ "(\n ", ( [ .[] | makeColumnItemText ] | join(",\n ") ), "\n)" ] | join("") ; # TableSpec -> string(format = "SQL") def makeCreateTableFromTableSpec: [ "CREATE TABLE IF NOT EXISTS \(.tableName) \n", (.columns | makeColumnListText), "\n;" ] | join("") ; # (name: string) : DbSpec -> string(format = "SQL") def main(name): . | getTableSpecFromDbSpec(name) # TableSpec or null | (if (. != null) then makeCreateTableFromTableSpec else "" end) ;
å®è¡ãã¦ã¿ãã¨ï¼
$ jq -r 'import "create-table" as DB; DB::main("bib")' dbspec.json CREATE TABLE IF NOT EXISTS bib ( id INTEGER NOT NULL PRIMARY KEY , title TEXT NOT NULL , author TEXT NOT NULL , date TEXT , pages TEXT , url TEXT , inmyblogpage TEXT ) ;
ãã®åºåã sqlite3 ã«ãã¤ãããã°ç®çã®ãã¼ãã«ãåºæ¥ä¸ããã¾ãã
$ jq -r 'import "create-table" as DB; DB::main("bib")' dbspec.json | sqlite3 bib.sqlite
ãããã«
jq ãå®éã«ä½¿ãä¸ã§ã®æ³¨æäºé ãã³ããç´¹ä»ãã¦ãªãæ©è½ã¯ã¾ã ããã®ã§ãããåççãªé¨åã¯ååã«èª¬æããã¨æãã¾ãããã¨ã¯ãjq ã®ããã¥ã¢ã« https://stedolan.github.io/jq/manual/ ãèªãã§ããã¬ã¤ã°ã©ã¦ã³ã https://jqplay.org/ ã§è©¦ãã¦ã¿ãã°ãjq ã使ããããã«ãªãã§ããããjq ã¯ãJSONå¦çã«ãããå¼·åãªãã¼ã«ã¨ãã¦å¨åãçºæ®ãã¦ããã¾ãã
*1:PowerShell ã®åé¡ã¨ãããããPowerShell ã®åºç¤ã§ãã .NET Framework ã®ããã¹ãIOã®ä»æ§ï¼so-called ãã°ã§ã¯ãªãï¼ã®ããã§ãã
*2:大æã®DOSã§ã¯ãããã¹ããã¡ã¤ã«ã®EOFãEnd Of Fileãã« ^Z æåã使ã£ã¦ãã¾ãããä»ã¯ãããã«ãªãã§ãããã
*3:ãã¡ãã¨ã¸ã§ãå¶å¾¡ã§ãã¦ããã©ããã¯ããããã
*4:大å¤ãªã®ã¯ãUnixã®ã³ã³ã½ã¼ã«ï¼ã¿ã¼ããã«ã»ã·ã¹ãã ã¨ã¯åºèªãéãWindowsã ãããç¥ãã¾ãããã
*5:ãä¸è¦ãã¯å ¥åã¯æ¨ã¦ããããã¨ã§ããã©ããæ¨ã¦ãããå ¥åãªããå¤é¨ããä¾çµ¦ããå¿ è¦ã¯ãªãã®ã§ãã³ãã³ãã©ã¤ã³ãªãã·ã§ã³ -n ã使ãã¾ãã
*6:length ã¯ããã¼ã«å¤ä»¥å¤ã®ä»»æã®JSONå¤ã«å¯¾ãã¦å®ç¾©ããã¦ãã¾ãã
*7:[追è¨]ç¨éãè¦ãæãã¯ãæ®éã«å¤æ°ã§ãããããã¼ã¿ã®ä¸æ¸ãå¤æ´ããã¦ãªãç¹ãã¹ã³ã¼ãã³ã°ãããååä»ããã¤ãã¨ï¼å°ãªãã¨ãã¡ã³ã¿ã«ã¢ãã«ã¨ãã¦ã¯ï¼æããã®ãé©åã ã¨æãã¾ãã[/追è¨]
*8:getpath é¢æ°ã¯ãåçã«ä½ã£ããã¹å¼ï¼ã«å¯¾å¿ããé åï¼ã«ããæ½åºã«ä½¿ãã¾ãã
*9:jq ã«ã¯ãé¢æ°å¼æ°ã®é«éé¢æ°ã¯ãªãã®ã§ãmap ã¯çµã¿è¾¼ã¿ã®æ§æã§ããâ ããã¯ã¦ã½ã§ãããé«éé¢æ°ãããç¨åº¦ä½¿ãã¾ãã.[] 㨠path ã¯çµã¿è¾¼ã¿æ§æã§ãã
*10:æ§æãã¤ã©ã¤ããã«ã©ã¼ãªã³ã°ã㯠TypeScript ã®ãã®ã使ç¨ãã¾ãããããã£ããã¡ããã¨è²ãä»ãã¦ãã¾ããã