jq 便å©ã§ãããã
jq 使ã£ã¦ã¾ããã¿ããªããæ´å½¢ç¨åº¦ã«ãã使ã£ã¦ãªãã£ãããçµè¾¼ã«ãã使ã£ã¦ãªãæ°ã¨æããã§ããã
jq ã§ããæ¡ä»¶ãæºãããªãã¸ã§ã¯ããåãåºããã
SQLã®select where
ã¿ããã« select {} where [].name = 'takuya'
ã¿ãã㪠jq ãããããæé«ãªãã§ããã
æ¡ä»¶ããããããã¼ããåãåºãä¾
$ echo '[ { "a":1 }, { "x": 1 } ]' | jq ' .[] | select(.x) ' { "x": 1 }
åºæ¥ããã§ããããã¥ã¢ã«ã«æãã¾ããããããããã¯æé«ã ã
åç´ãªã¨ãããããè¦ã¦ãã
ãããªã select è¦ã¦ãä½ã®ãã¨ãããããªãã®ã§ãåç´ãªã¨ããããè¦ã¦ãããã¨ã«ããã
åç´ãªæ´å½¢
jq ã使ã£ã¦æ´å½¢ãããããããä¾ã§ãããã㯠jq .
ã¨æ¸ãã¦ã¾ããã¤ã¾ãã«ã¬ã³ããã¼ããåºåããã£ã¦æå³ã§ãã
åç´ãªæ´å½¢ã®ä¾
$ echo '[{},{}]' | jq . [ {}, {} ]
é åã®ä¸èº«ãåºã
é
åã®ä¸ãèªåã§èµ°æ»ãã¦ä¸èº«ãåãåºãã¦ãããã®ãããã¡ãã«ã«ãªãã¾ãã jq .[]
㯠jq ã®æä¸ä½ã®ãã¼ãã®ã²ã¨ã¤ä¸ã®é
åãIterationãã¦ä¸èº«ãããã£ã¦äºã«ãªãã¾ãã
é åã®ä¸èº«ãåºååãåºãä¾
$ echo '[1,2,3]' | jq '.[]' 1 2 3
é åã®æå®ããINDEXãåãåºãã
é åã®ãªãã«ãã¤ã³ããã¯ã¹ãæå®ããã¨ãæå®ããçªå·ã®ãã®ãåãåºããã
é åã®æå®ããINDEXãåãåºãä¾
$ echo '[1,2,3]' | jq '.[1]' 2
ããã«ããã¯ãä¸ã¤ã ããããªãããã«çªã£è¾¼ãã æ¸ãæ¹ãã§ããã
é åã®ä¸èº«ã®é çªãè¤æ°æå®ãã¦ãã¾ãä¾
$ echo '[1,2,3]' | jq '.[0,2,1]' # é åºã 1 3 2 $ echo '[1,2,3]' | jq '.[0,2]' # é¸æã 1 3
è¦ç´ ããªãã¸ã§ã¯ã㧠ããã·ã¥ãã¼ããåãåºã
é¸æ対象ã¨ãªãè¦ç´ ãããã·ã¥ï¼ãªãã¸ã§ã¯ãï¼ã§ãã®ä¸ãStringããã¼ã«åãåºãã¦ããããã¨ãã
ãªãã¸ã§ã¯ãã®ä¸èº«ã ããåãåºãããã
$ echo '{ "a":1 , "b":2 }' | jq '.a' 1 $ echo '{ "a":1 , "b":2 }' | jq '.b' 2
ãã¹ããããªãã¸ã§ã¯ãã§ã大ä¸å¤«ã
# ãã¹ã ï¼æ®µ ã»ãã¹ãï¼æ®µ
$ echo '{ "a": { "x": 1 }, "b":2 }' | jq '.a' { "x": 1 } $ echo '{ "a": { "x": 1 }, "b":2 }' | jq '.a.x'ã#ãã¹ã ï¼æ®µ 1
é åã¨ãªãã¸ã§ã¯ãã®é¸æã®çµã¿åããã
ããã¾ã§è¦ãé
åã¨ãªãã¸ã§ã¯ãã®æå®ãjq
ã§è¡ãå ´åãçµã¿åãããã¨æ¬¡ã®ããã«ãªãã
é åã¨ãªãã¸ã§ã¯ããåæã«ä½¿ãã
$ echo '[ { "a":1 }, { "x": 1 } ]' | jq '.' # ã¾ãã¯åç´ã«æ´å½¢ [ { "a": 1 }, { "x": 1 } ] $ echo '[ { "a":1 }, { "x": 1 } ]' | jq '.[]' # é åè¦ç´ ãã¨ã«print { "a": 1 } { "x": 1 } $ echo '[ { "a":1 }, { "x": 1 } ]' | jq '.[].a' # é åã®è¦ç´ å ã§æ¤ç´¢ 1
true/falseã«å¤å½¢ï¼æ¤ç´¢æ¡ä»¶ãæ¸ã
ããã§ãæ´ã«æ¤ç´¢æ¡ä»¶ã足ãã¦ãæ¸ãæãå¦çãè¡ããmap å¦çã§ãã
é åã®åè¦ç´ ããã®å¤ã«ãã£ã¦æ¸ãæãã¦ããã¾ãã
jq ã§è¦ç´ ã®ãªãã¸ã§ã¯ããæ¸ãæã true/falseã«ãã
$ echo '[ { "a":1 }, { "x": 1 } ]' | jq '(.[].a==1)' true false
æå¾ã«select ã¨çµã¿åãããã
true / false ã«å¤å½¢ãããã¨ãåºæ¥ããããããã£ã«ã¿ããã map /reduce / filter
ã®æ¦å¿µãç解ã§ãã¦ãã°ããããã§ãã
$ echo '[ { "a":1 }, { "x": 1 } ]' | jq '.[] | select (.a==1)' # a == 1 ã®ãã®ã ã { "a": 1 } $ echo '[ { "a":1 }, { "x": 1 } ]' | jq ' .[] | select(.x) ' # 'x': object ãåå¨ãããã®ã ã { "x": 1 }
å人çææ³
jq 便å©ããã¦ãã°ããSTDIOã¨ãã¤ããJSONã¨çµã¿åãããxargs ã«ãããæ°ããæ¦å¿µãããããªãã
åèè³æ
ã¾ã«ã ããã«ãã£ãããã