Linux下的jq命令处理json数据

jq命令可以过滤json格式数据

1
2
3
4
安装   https://stedolan.github.io/jq/
wget -O jq https://github.com/stedolan/jq/releases/download/jq-1.5/jq-linux64
chmod +x ./jq
cp jq /usr/bin
操作演示

cat test.json | jq

mark

cat 1.json |jq -r '.timeDelay[].duration' 过滤出duration里的数据
mark

1
2
3
4
cat 1.json |jq -r '.timeDelay[] |select (.url | contains(“http:/as/index”))'
contains包含这个值的全部匹配
cat 1.json |jq -r '.timeDelay[] |select (.url == “http:/as/index”)'
== 等于这个值的匹配

mark

有个弊端就是不支持key是数字的

1
2
3
4
5
6
7
8
9
10
11
# 批量过滤数据脚本
#!/bin/bash
cat api_url.txt |while read line
do
id=$(echo $line |awk -F'/' '{print $2"-"$3}')
if [ $id == "-" ];then
id=1
fi
# cat 1.json |jq -r '.timeDelay[] |select (.url | contains("'$line'"))' > /data/script/api_url/${id}.json
cat 1.json |jq -r '.timeDelay[] |select (.url == "'$line'")' > /data/script/api_url/${id}.json
done
-------------本文结束感谢您的阅读-------------
原创技术分享,感谢您的支持。