yum -y install jq
openstack 命令行 应用
SELFSERVICE_NET_ID=`openstack network show selfservice -f json | jq .id | sed 's/\"//g'`
docker 命令行 应用
docker inspect test1 | jq .[0].Config.Vomumes
内建函数
jq -r '.nameInfo[].lastName'
jq '.data.volumes[0].chapters|length'
cat json_raw.txt | jq 'has("name")'
cat json_raw.txt | jq 'keys'
获取docker 容器 日志路径
sudo docker inspect fs.18021 | jq .[0].LogPath
去掉日志路径外面的双引号
sudo docker inspect fs.18021 | jq .[0].LogPath | sed 's/\"//g'
docker inspect xxx | jq .[0].Config.Env
清除docker 容器 日志
sudo sh -c 'echo "" > /docker-data/containers/19667cf14b858e16643b196f2326f5bc205c6db7ad8ecf08cb6cc350fc97fba9/19667cf14b858e16643b196f2326f5bc205c6db7ad8ecf08cb6cc350fc97fba9-json.log'
jq -r
jq 遍历数组
cat apps.json |jq -r ".apps[$x].appName"
cat ./test.json
[
"192.168.100.106",
"192.168.100.107"
]
cat test.json | jq -r .[$x]
获取指定字段
kubectl get ipamblocks.crd.projectcalico.org -ojson|jq '.items[]|{subnet: .spec.cidr, nodeInfo: .spec.affinity}'
把数据当作数组处理 jq -s
cat data.json
{
"subnet": "10.244.187.0/26",
"nodeInfo": "host:11.1.0.250"
}
{
"subnet": "10.244.223.0/26",
"nodeInfo": "host:12.1.0.252"
}
{
"subnet": "10.244.229.192/26",
"nodeInfo": "host:11.1.0.251"
}
{
"subnet": "10.244.240.64/26",
"nodeInfo": "host:12.1.0.253"
}
cat data.json | jq -s
[
{
"subnet": "10.244.187.0/26",
"nodeInfo": "host:11.1.0.250"
},
{
"subnet": "10.244.223.0/26",
"nodeInfo": "host:12.1.0.252"
},
{
"subnet": "10.244.229.192/26",
"nodeInfo": "host:11.1.0.251"
},
{
"subnet": "10.244.240.64/26",
"nodeInfo": "host:12.1.0.253"
}
]
将json 数据带入脚本
cat data.json
{
"subnet": "10.244.187.0/26",
"nodeInfo": "host:11.1.0.250"
}
{
"subnet": "10.244.223.0/26",
"nodeInfo": "host:12.1.0.252"
}
{
"subnet": "10.244.229.192/26",
"nodeInfo": "host:11.1.0.251"
}
{
"subnet": "10.244.240.64/26",
"nodeInfo": "host:12.1.0.253"
}
num=`cat data.json | jq -s length`;for ((i=0;i<num;i++));do sub=`cat data.json | jq -s .[$i].subnet`; node=`cat data.json | jq -s .[$i].nodeInfo | sed 's/host://g'`;echo $sub $node ;done
查看容器环境斌量
docker inspect xxx | jq .[0].Config.Env
查看一个object 有多少字段
cat test.json
{
"key-a": "xxx-a",
"key-b": "xxx-b",
"key-c": "xxx-c",
"key-d": "xxx-d",
"key-3": "xxx-3",
"key-f": "xxx-f",
"key-g": "xxx-g"
}
jq 'length' test.json
cat test.json | jq 'length'
cat test.json | jq '. | length'
打印object key value
cat test.json
{
"key-a": "xxx-a",
"key-b": "xxx-b",
"key-c": "xxx-c",
"key-d": "xxx-d",
"key-3": "xxx-3",
"key-f": "xxx-f",
"key-g": "xxx-g"
}
cat test.json | jq -r '. | keys[] as $k | "\($k),\(.[$k] | .)"'
key-3,xxx-3
key-a,xxx-a
key-b,xxx-b
key-c,xxx-c
key-d,xxx-d
key-f,xxx-f
key-g,xxx-g
object 中的键值带入脚本
cat test.json
{
"key-a": "xxx-a",
"key-b": "xxx-b",
"key-c": "xxx-c",
"key-d": "xxx-d",
"key-3": "xxx-3",
"key-f": "xxx-f",
"key-g": "xxx-g"
}
num=$(cat test.json | jq length)
echo $num
7
for ((i=0;i<num;i++));do key_for_jq=$(jq keys[$i] test.json); key_for_sed=$(jq -r keys[$i] test.json); value=$(jq -r ."$key_for_jq" test.json); echo "$key_for_sed" "$value"; done
key-3 xxx-3
key-a xxx-a
key-b xxx-b
key-c xxx-c
key-d xxx-d
key-f xxx-f
key-g xxx-g