linux-shell命令處理json數據
0x00 jq介紹
jq是一個輕量級的靈活的處理json對象的命令。支持linux、windows
0x01 二進位安裝
wget -O jq https://github.com/stedolan/jq/releases/download/jq-1.5/jq-linux64
chmod +x ./jq
cp jq /usr/bin
0x02 使用yum安裝
yum -y install epel-release
yum -y install jq
0x03 使用
1、格式化輸出
curl //ip.taobao.com/service/getIpInfo.php?ip=8.8.8.8 2>> /dev/null | jq
{
"code": 0,
"data": {
"ip": "8.8.8.8",
"country": "美國",
"area": "",
"region": "XX",
"city": "XX",
"county": "XX",
"isp": "Level3",
"country_id": "US",
"area_id": "",
"region_id": "xx",
"city_id": "xx",
"county_id": "xx",
"isp_id": "200053"
}
}
2、獲取指定欄位的數據
curl //ip.taobao.com/service/getIpInfo.php?ip=8.8.8.8 2>> /dev/null | jq -r ".code,.data.ip,.data.country"
8.8.8.8
美國
3、提取指定的欄位保存成csv文件
curl //ip.taobao.com/service/getIpInfo.php?ip=8.8.8.8 2>> /dev/null | jq -r "[(.code|tostring),.data.ip,.data.country]|join(",")"
0,8.8.8.8,美國
注釋:
.code|tostring:取code欄位的值並轉化為string類型
[]:數組
join(","):數組中的元素通過","連接。
4、csv轉換成json字元串
cat /tmp/1.csv
0,8.8.8.8,美國
0,8.8.8.7,美國
0,8.8.2.7,美國
0,81.8.2.7,土耳其
jq -R "split(",")|{"code":.[0],"ip":.[1],"country":.[2]}" /tmp/1.csv
{
"code": "0",
"ip": "8.8.8.8",
"country": "美國"
}
{
"code": "0",
"ip": "8.8.8.7",
"country": "美國"
}
{
"code": "0",
"ip": "8.8.2.7",
"country": "美國"
}
{
"code": "0",
"ip": "81.8.2.7",
"country": "土耳其"
}
5、 -c/--compact-output:jq 默認是pretty-prints JSON 輸出,加上這個參數,一行一個json對象。
jq -R -c "split(",")|{"code":.[0],"ip":.[1],"country":.[2]}" /tmp/1.csv
{"code":"0","ip":"8.8.8.8","country":"美國"}
{"code":"0","ip":"8.8.8.7","country":"美國"}
{"code":"0","ip":"8.8.2.7","country":"美國"}
{"code":"0","ip":"81.8.2.7","country":"土耳其"}
6、複雜json查詢
complexJson="{"uid":"777","nameInfo":[{"firstName":"Jim","lastName":"Green"},{"firstName":"Lei","lastName":"Li"}]}"
echo $ |jq
{
"uid": "777",
"nameInfo": [
{
"firstName": "Jim",
"lastName": "Green"
},
{
"firstName": "Lei",
"lastName": "Li"
}
]
}
echo $ |jq -r ".nameInfo[].lastName"
結果:
Green
Li
注釋:
.nameInfo[].lastName是獲取.nameInfo[] json array中每個json object的lastName欄位。
※linux-shell編程中awk變數的使用
※linux-RHEL/Centos都選擇了xfs文件系統
TAG:linux運維菜 |