Linux格式化显示json工具jq
Linux格式化显示json工具jq
安装jq
# 安装jq
dnf install jq -y
格式化显示json
echo 'json格式的数据' | jq

读取json文件
# 读取json文件
cat tmp.json | jq

获取所有的key
# 获取json中所有的key
cat tmp.json |jq -r keys

获取制定key的value值
# cat tmp.json |jq '.key值.key值'
cat tmp.json |jq '.location.city'

根据条件过滤JSON数据
# 过滤指定key-value的数据
cat log.json | jq '.[] | select(.location.city == "value")'
本地安装jq失败
本地安装jq时提示: nothing provides libonig.so.5()(64bit) needed by jq-1.6-2.el7.x86_64

解决办法,离线下载jq依赖包oniguruma
到一台有外网的机器上下载oniguruma包,然后拷贝到本地再进行安装
# 下载oniguruma包
yumdownloader --resolve oniguruma
# 拷贝到本地机器
scp oniguruma-6.8.2-2.el7.x86_64.rpm 本地无公网机器:/root/
# 安装jq的依赖包oniguruma-6.8.2-2.el7.x86_64.rpm
yum install -y oniguruma-6.8.2-2.el7.x86_64.rpm jq