jq
在 shell 環境下, 用來解析 json 字串的工具, 類似 sed
, awk
, grep
的字串處理工具。
https://stedolan.github.io/jq/
jq play
測試 jq 語法。
https://jqplay.org/
基本用法
格式化 response 方便閱讀。
curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.' |
取得 element 0
curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.[0]' |
取得 element 0, 並且將 .commit.message
的字串重新組合到 message
, .commit.committer.name
重新組合到 name
curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.[0] | {message: .commit.message, name: .commit.committer.name}' |
可以用 []
collection (array) 的形式, 打包 json 的處理結果
curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '[.[] | {message: .commit.message, name: .commit.committer.name}]' |
取得 docker bridge 的 ipv4
docker network inspect bridge |
[ |
先把 Containers
先存起來
containers=$(docker network inspect bridge | jq '.[0].Containers' | jq 'keys[]') |
再拿來 select 一次
ipv4=$(docker network inspect bridge | jq ".[0].Containers.${containers}.IPv4Address") |