什么是jq
jq is like sed for JSON data - you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text.
安裝前在命令行使用的效果
安裝后再命令行使用的效果
安裝前
最簡單的方法是使用homebrew安裝役纹,所以先安裝homebrew
安裝homebrew
- 在命令行中輸入安裝命令
詳見homebrewruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
安裝完成后博投,可使用命令brew doctor 來檢查是否安裝成功brew doctor
安裝jq
- 一鍵安裝
brew install jq
- 若使用brew報(bào)錯(cuò)但汞,可卸載homebrew后重新安裝
使用
常用方法
- 格式化
curl 'http://xxx.xxx.com/v1/GoodsTag/getAllTagList' -s | jq '.'
- 獲取特定字段:.特定字段
curl 'http://xxx.xxx.com/v1/GoodsTag/getAllTagList' -s | jq '.response_status'
以豆瓣api為例子
{
"id":"1220562",
"alt":"http:\/\/book.douban.com\/book\/1220562",
"rating":{"max":10, "average":"7.0", "numRaters":282, "min":0},
"author":[{"name":"片山恭一"}, {"name":"豫人"}],
"alt_title":"",
"image":"http://img3.douban.com\/spic\/s1747553.jpg",
"title":"滿月之夜白鯨現(xiàn)",
"mobile_link":"http:\/\/m.douban.com\/book\/subject\/1220562\/",
"summary":"那一年拔稳,是聽莫扎特悟狱、釣鱸魚和家庭破裂的一年玄糟。說到家庭破裂此蜈,母親怪自己當(dāng)初沒有找到好男人即横,父親則認(rèn)為當(dāng)時(shí)是被狐貍精迷住了眼,失常的是母親裆赵,但出問題的是父親……东囚。",
"attrs":{
"publisher":["青島出版社"],
"pubdate":["2005-01-01"],
"author":["片山恭一", "豫人"],
"price":["18.00元"],
"title":["滿月之夜白鯨現(xiàn)"],
"binding":["平裝(無盤)"],
"translator":["豫人"],
"pages":["180"]
},
"tags":[
{"count":106, "name":"片山恭一"},
{"count":50, "name":"日本"},
{"count":42, "name":"日本文學(xué)"},
{"count":30, "name":"滿月之夜白鯨現(xiàn)"},
{"count":28, "name":"小說"},
{"count":10, "name":"愛情"},
{"count":7, "name":"純愛"},
{"count":6, "name":"外國文學(xué)"}
]
}
-
數(shù)組操作
- 獲取數(shù)組tags中第一個(gè)元素count
curl 'https://api.douban.com/v2/book/1220562' -s | jq '.tags[0].count'
-
重新組合
- 將tag數(shù)組中的元素count,name 輸出為code, mingzi,
|:the output of one filter into the input of another战授,類似管道符
curl 'https://api.douban.com/v2/book/1220562' -s | jq '.tags[] | {code:.count, mingzi:.name'
- 結(jié)果
- 將tag數(shù)組中的元素count,name 輸出為code, mingzi,
{
"code": 138,
"mingzi": "片山恭一"
}
{
"code": 65,
"mingzi": "日本"
}
{
"code": 61,
"mingzi": "日本文學(xué)"
}
{
"code": 38,
"mingzi": "小說"
}
{
"code": 32,
"mingzi": "滿月之夜白鯨現(xiàn)"
}
{
"code": 15,
"mingzi": "愛情"
}
{
"code": 8,
"mingzi": "純愛"
}
{
"code": 8,
"mingzi": "外國文學(xué)"
}
- 獲取多個(gè)結(jié)果 ,分隔多個(gè)結(jié)果
- 命令行輸入
curl 'https://api.douban.com/v2/book/1220562' -s | jq '.tags[0].count页藻, .tags[0].name'
- 結(jié)果:
138
片山恭一
- 長度
- JSON 對象
curl 'https://api.douban.com/v2/book/1220562' -s | jq '.| length'
- JSON 數(shù)組
curl 'https://api.douban.com/v2/book/1220562' -s | jq '.tags | length'
- 字符串
curl 'https://api.douban.com/v2/book/1220562' -s | jq '.summary | length'
- keys
curl 'https://api.douban.com/v2/book/1220562' -s | jq '.keys'
[
"alt",
"alt_title",
"author",
"author_intro",
"binding",
"catalog",
"id",
"image",
"images",
"isbn10",
"isbn13",
"origin_title",
"pages",
"price",
"pubdate",
"publisher",
"rating",
"subtitle",
"summary",
"tags",
"title",
"translator",
"url"
]
- map(foo)
echo '[1,5,3]' | jq 'map(.+10)'
[ 11, 15, 13]
- select(foo)
echo '[1,5,3,0,7]' | jq 'map(select(.<2))'
[ 1, 0]