1.簡介
? ? jq 是一款非常強大的 JSON 命令列處理工具,其官網地址為:https://stedolan.github.io/jq/
2.安裝
? ? 以CentOS為例:
1.在線安裝
yum install -y epel-release && yum install -y jq
2.離線安裝
- 訪問官網,并下載jq(Linux 64-bit)
- 在Linux中執行命令
mv -f /home/surpass/jq-linux64 /usr/bin/jq
3.驗證安裝
# jq -h
Some of the options include:
-c compact instead of pretty-printed output;
-n use `null` as the single input value;
-e set the exit status code based on the output;
-s read (slurp) all inputs into an array; apply filter to it;
-r output raw strings, not JSON texts;
-R read raw strings, not JSON texts;
-C colorize JSON;
-M monochrome (don't colorize JSON);
-S sort keys of objects on output;
--tab use tabs for indentation;
--arg a v set variable $a to value <v>;
--argjson a v set variable $a to JSON value <v>;
--slurpfile a f set variable $a to an array of JSON texts read from <f>;
--rawfile a f set variable $a to a string consisting of the contents of <f>;
--args remaining arguments are string arguments, not files;
--jsonargs remaining arguments are JSON arguments, not files;
-- terminates argument processing;
3.常用引數
- -h/--help
輸出jq幫助資訊
- -V/--version
輸出jq版本資訊
- -c/--compact-output
將每個JSON物件在單行內以緊湊的形式輸出
- -n/--null-input
不讀取任何輸入,將null做為輸入運行一次
- -r/--raw-output
在開啟這個選項的情況下,如果 過濾器 的結果是 string,就會直接寫入標準輸出而不是以 JSON string 的格式輸出,這在 jq 過濾器和其他處理 非-JSON 系統互動時比較有用,
- -R/--raw-input
不要將輸入決議為JSON,相反,每行文本都以字串形式傳遞給過濾器
- -C/--color-output和-M/--monochrome-output
默認情況下,如果是寫入到終端,jq 會輸出 colored JSON ,也可以使用 -C 強制輸出彩色的JSON 到管道或者檔案,也可以使用 -M 禁掉輸出 colored JSON ,
- -S/--sort-keys
將每個 JSON object 的各個欄位按照 key 排序的順序輸出
- --tab
使用tab符做為縮進符
- --indent n
指定縮進使用的空格數,范圍為[-1,7]
4.基本過濾器
? ? 在使用過濾器前,先來看看一個示例資料,如下所示:
{
"personInfo": {
"name": "Surpass",
"age": 28,
"location": "shanghai"
},
"others": [
"a",
"b",
"c"
]
}
- 1、.
? ? 最簡單也是最平常的過濾器,其作用是接收輸入并原樣輸出的過濾器
cat test.json | jq .
{
"personInfo": {
"name": "Surpass",
"age": 28,
"location": "shanghai"
},
"others": [
"a",
"b",
"c"
]
}
- 2、.key 或 .key.key 或 '.["key"]'
? ? 獲取指定key的值,如果沒有找到指定的key,則輸出null
# cat test.json | jq .personInfo
{
"name": "Surpass",
"age": 28,
"location": "shanghai"
}
# cat test.json | jq .personInfo.name
"Surpass"
# cat test.json | jq '.["personInfo"]'
{
"name": "Surpass",
"age": 28,
"location": "shanghai"
}
# cat test.json | jq .abc
null
- 3、.key?
? ? 使用 .key 時,當 . 不是一個陣列或物件時會而報錯,使用 .key? 則不會報錯
# echo "1" | jq .a
jq: error (at <stdin>:1): Cannot index number with string "a"
# echo "1" | jq .a?
#
- 4、.[index] 或 .[startIndex:endIndex]
? ? 獲取物件或value為陣列或字串的子陣列或子符串,索引主要有以下幾種形式
- 索引下標從0開始,
- 支持負索引,-1表示最后一個元素,-2表示倒數第二個元素
# cat test.json | jq .others[0]
"a"
# cat test.json | jq .others[1:3]
[
"b",
"c"
]
# cat test.json | jq .others[-3:-1]
[
"a",
"b"
]
# cat test.json | jq .others[:3]
[
"a",
"b",
"c"
]
# echo '[{"name":"Surpass","age":28},{"oters":1234}]' | jq .[0]
{
"name": "Surpass",
"age": 28
}
# echo '["Surpass"]' | jq .[0][0:3]
"Sur"
- 5、.[]
? ? 獲取所有的value值
# cat test.json | jq .[]
{
"name": "Surpass",
"age": 28,
"location": "shanghai"
}
[
"a",
"b",
"c"
]
- 6、keys
? ? 獲取所有的key物件
# cat test.json | jq keys
[
"others",
"personInfo"
]
- 7、[.[]]
? ? 獲取所有value組成的陣列
cat test.json | jq [.[]]
[
{
"name": "Surpass",
"age": 28,
"location": "shanghai"
},
[
"a",
"b",
"c"
]
]
- 8、.[].key
? ? 獲取陣列元素中指定key的所有值
# echo '[{"name":"Surpass"},{"name":"Kevin"},{"name":"Tina"}]' | jq .[].name
"Surpass"
"Kevin"
"Tina"
如果需要將輸出結果再次組裝為陣列,可以這樣使用 [.[].key]
- 9、,
? ? 使用多個篩選條件
# cat test.json | jq .personInfo | jq [.name,.age]
[
"Surpass",
28
]
- 10、管道
? ? 可以通過 | 實作管道功能,從而達到對處理的結果進行二次或多次處理
# cat test.json | jq '.personInfo|.name,.age'
"Surpass"
28
# cat test.json | jq '.others|.[2]'
"c"
也可以使用Linux自帶的 | 實作同樣的功能,
- 11、length
? ? length 可以獲取字串或陣列的長度
# cat test.json | jq '.others|length'
3
# cat test.json | jq '.personInfo|length'
3
# cat test.json | jq '.personInfo|.name|length'
7
- 12、map
? ? 可以實作對陣列的每一項操作,然后進行合并結果,
# echo '["Surpass","Kevin","Tina"]' | jq 'map(length)'
[
7,
5,
4
]
- 13、filter(select)
? ? 可以實作對輸入項進行判斷,然后僅回傳符合條件的項
# echo '["Surpass","Kevin","Tina"]' | jq 'map(select(.|length>5))'
[
"Surpass"
]
# echo '["Surpass","Kevin","Tina"]' | jq 'map(select(.|length>=5))'
[
"Surpass",
"Kevin"
]
- 14、\()
? ? 可以實作字串插值功能
# cat test.json | jq '"Hello \(.personInfo|.name)"'
"Hello Surpass"
# echo '{"name":"Surpass","age":28}' | jq '"age is: \(.age)"'
"age is: 28"
- 15、+
? ? 可以實作字串拼接功能
# echo '{"name":"Surpass","age":28}' | jq '"name is :" + .name'
"name is :Surpass"
cat test.json | jq '"name is :" + .personInfo.name '
"name is :Surpass"
- 16、if/elif/else
? ? 可以使用 if .. then .. elif .. then .. else .. end 實作條件判斷
# echo '[0, 1, 2, 3]' \
> | jq 'map(if . == 0 then "zero" elif . == 1 then "one" elif . == 2 then "two" else "many" end)'
[
"zero",
"one",
"two",
"many"
]
- 17、構造 object 或陣列
? ? 可以通過 {} 和 [] 構造新的 object 或 陣列
- object:
# echo '["Surpass","Kevin","Tina"]' | jq '{name:.[0]}'
{
"name": "Surpass"
}
# echo '{"name":"Surpass","ages":[28,29,30]}' | jq '{name,age: .ages[]}'
{
"name": "Surpass",
"age": 28
}
{
"name": "Surpass",
"age": 29
}
{
"name": "Surpass",
"age": 30
}
- array
# echo '{"name":"Surpass","age":28,"location":"Shanghai"}' | jq '[.name,.age,.location]'
[
"Surpass",
28,
"Shanghai"
]
- 18、join
? ? 可以進行陣列拼接功能,
# echo '["Surpass","28","Shanghai"]' | jq '.|join("--")'
"Surpass--28--Shanghai"
- 19、split
? ? 可對字串進行拆分
# echo '"Surpass--28--Shanghai"' | jq 'split("--")'
[
"Surpass",
"28",
"Shanghai"
]
原文地址:https://www.jianshu.com/p/2f58c975b9ca
本文同步在微信訂閱號上發布,如各位小伙伴們喜歡我的文章,也可以關注我的微信訂閱號:woaitest,或掃描下面的二維碼添加關注:

作者: Surpassme
來源: http://www.jianshu.com/u/28161b7c9995/
http://www.cnblogs.com/surpassme/
宣告:本文著作權歸作者所有,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出 原文鏈接 ,否則保留追究法律責任的權利,如有問題,可發送郵件 聯系,讓我們尊重原創者著作權,共同營造良好的IT朋友圈,
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/500339.html
標籤:其他
上一篇:firewall 命令簡單操作
下一篇:mysql進階
