前言
jq類似一個awk或grep一樣的神器,可以方便地在命令行操作json
這里我使用海南萬寧的天氣接口做演示,地址:http://t.weather.sojson.com/api/weather/city/101310215
一、安裝
官網:https://stedolan.github.io/jq/download/
基本就是brew install、apt install、yum install之類的,很簡單
二、常用操作
1.格式化json
直接請求,格式明顯不易讀
➜ ~ curl -s http://t.weather.sojson.com/api/weather/city/101310215
{"message":"success感謝又拍雲(upyun.com)提供CDN贊助","status":200,"date":"20191025","time":"2019-10-25 11:17:41","cityInfo":{"city":"萬寧市","citykey":"101310215","parent":"海南","updateTime":"09:59"},"data":{"shidu":"92%","pm25":20.0,"pm10":49.0,"quality":"優","wendu":"25","ganmao":"各類人群可自由活動","forecast":[{"date":"25","high":"高溫 29℃","low":"低溫 23℃","ymd":"2019-10-25","week":"星期五","sunrise":"06:35","sunset":"18:10","aqi":44,"fx":"無持續風向","fl":"<3級","type":"中雨","notice":"記得隨身攜帶雨傘哦"},{"date":"26","high":"高溫 29℃","low":"低溫 23℃","ymd":"2019-10-26","week":"星期六","sunrise":"06:35","sunset":"18:09","aqi":31,"fx":"無持續風向","fl":"<3級","type":"小雨","notice":"雨雖小,注意保暖別感冒"},{"date":"27","high":"高溫 29℃","low":"低溫 23℃","ymd":"2019-10-27","week":"星期日","sunrise":"06:36","sunset":"18:09","aqi":28,"fx":"無持續風向","fl":"<3級","type":"小雨","notice":"雨雖小,注意保暖別感冒"},{"date":"28","high":"高溫 30℃","low":"低溫 22℃","ymd":"2019-10-28","week":"星期一","sunrise":"06:36","sunset":"18:08","aqi":34,"fx":"東北風","fl":"3-4級","type":"小雨","notice":"雨雖小,注意保暖別感冒"},{"date":"29","high":"高溫 27℃","low":"低溫 22℃","ymd":"2019-10-29","week":"星期二","sunrise":"06:36","sunset":"18:07","aqi":35,"fx":"東北風","fl":"3-4級","type":"小雨","notice":"雨雖小,注意保暖別感冒"},{"date":"30","high":"高溫 27℃","low":"低溫 22℃","ymd":"2019-10-30","week":"星期三","sunrise":"06:37","sunset":"18:07","aqi":32,"fx":"無持續風向","fl":"<3級","type":"中雨","notice":"記得隨身攜帶雨傘哦"},{"date":"31","high":"高溫 29℃","low":"低溫 22℃","ymd":"2019-10-31","week":"星期四","sunrise":"06:37","sunset":"18:06","fx":"東北風","fl":"3-4級","type":"中雨","notice":"記得隨身攜帶雨傘哦"},{"date":"01","high":"高溫 25℃","low":"低溫 24℃","ymd":"2019-11-01","week":"星期五","sunrise":"06:38","sunset":"18:06","fx":"東北風","fl":"3-4級","type":"小雨","notice":"雨雖小,注意保暖別感冒"},{"date":"02","high":"高溫 25℃","low":"低溫 24℃","ymd":"2019-11-02","week":"星期六","sunrise":"06:38","sunset":"18:05","fx":"東北風","fl":"4-5級","type":"小雨","notice":"雨雖小,注意保暖別感冒"},{"date":"03","high":"高溫 26℃","low":"低溫 24℃","ymd":"2019-11-03","week":"星期日","sunrise":"06:38","sunset":"18:05","fx":"東風","fl":"3-4級","type":"小雨","notice":"雨雖小,注意保暖別感冒"},{"date":"04","high":"高溫 25℃","low":"低溫 24℃","ymd":"2019-11-04","week":"星期一","sunrise":"06:39","sunset":"18:04","fx":"東北風","fl":"<3級","type":"小雨","notice":"雨雖小,注意保暖別感冒"},{"date":"05","high":"高溫 28℃","low":"低溫 24℃","ymd":"2019-11-05","week":"星期二","sunrise":"06:39","sunset":"18:04","fx":"東南風","fl":"<3級","type":"小雨","notice":"雨雖小,注意保暖別感冒"},{"date":"06","high":"高溫 25℃","low":"低溫 21℃","ymd":"2019-11-06","week":"星期三","sunrise":"06:40","sunset":"18:04","fx":"東北風","fl":"3-4級","type":"小雨","notice":"雨雖小,注意保暖別感冒"},{"date":"07","high":"高溫 23℃","low":"低溫 22℃","ymd":"2019-11-07","week":"星期四","sunrise":"06:40","sunset":"18:03","fx":"東北風","fl":"3-4級","type":"小雨","notice":"雨雖小,注意保暖別感冒"},{"date":"08","high":"高溫 25℃","low":"低溫 22℃","ymd":"2019-11-08","week":"星期五","sunrise":"06:41","sunset":"18:03","fx":"北風","fl":"3-4級","type":"多雲","notice":"陰晴之間,謹防紫外線侵擾"}],"yesterday":{"date":"24","high":"高溫 29℃","low":"低溫 23℃","ymd":"2019-10-24","week":"星期四","sunrise":"06:34","sunset":"18:10","aqi":27,"fx":"無持續風向","fl":"<3級","type":"多雲","notice":"陰晴之間,謹防紫外線侵擾"}}}
使用jq格式化,如下
curl -s http://t.weather.sojson.com/api/weather/city/101310215 | jq
{
"message": "success感謝又拍雲(upyun.com)提供CDN贊助",
"status": 200,
"date": "20191025",
"time": "2019-10-25 10:27:28",
"cityInfo": {
"city": "萬寧市",
"citykey": "101310215",
"parent": "海南",
"updateTime": "09:59"
},
"data": {
"shidu": "92%",
"pm25": 20,
"pm10": 49,
"quality": "優",
"wendu": "25",
"ganmao": "各類人群可自由活動",
"forecast": [
{
"date": "25",
"high": "高溫 29℃",
"low": "低溫 23℃",
"ymd": "2019-10-25",
"week": "星期五",
"sunrise": "06:35",
"sunset": "18:10",
"aqi": 44,
"fx": "無持續風向",
"fl": "<3級",
"type": "中雨",
"notice": "記得隨身攜帶雨傘哦"
},...
已經格式化過
2.取指定字段的值
直接輸入字段,使用.嵌套訪問,例如要獲取昨天的信息
curl -s http://t.weather.sojson.com/api/weather/city/101310215 | jq .data.yesterday
{
"date": "24",
"high": "高溫 29℃",
"low": "低溫 23℃",
"ymd": "2019-10-24",
"week": "星期四",
"sunrise": "06:34",
"sunset": "18:10",
"aqi": 27,
"fx": "無持續風向",
"fl": "<3級",
"type": "多雲",
"notice": "陰晴之間,謹防紫外線侵擾"
}
3.過濾指定字段
使用一個json指定所有字段,如{date, high},獲取昨天的最高溫度如下
curl -s http://t.weather.sojson.com/api/weather/city/101310215 | jq .data.yesterday|jq "{date,high}"
{
"date": "24",
"high": "高溫 29℃"
}
4.獲取多個字段的值
使用逗號獲取多個
curl -s http://t.weather.sojson.com/api/weather/city/101310215 | jq .data.yesterday|jq ".date, .high"
"24"
"高溫 29℃"
5.篩選數組
直接指定數組的索引即可
curl -s http://t.weather.sojson.com/api/weather/city/101310215 |jq ".data.forecast"|jq ".[0,9]"
{
"date": "25",
"high": "高溫 29℃",
"low": "低溫 23℃",
"ymd": "2019-10-25",
"week": "星期五",
"sunrise": "06:35",
"sunset": "18:10",
"aqi": 44,
"fx": "無持續風向",
"fl": "<3級",
"type": "中雨",
"notice": "記得隨身攜帶雨傘哦"
}
{
"date": "03",
"high": "高溫 26℃",
"low": "低溫 24℃",
"ymd": "2019-11-03",
"week": "星期日",
"sunrise": "06:38",
"sunset": "18:05",
"fx": "東風",
"fl": "3-4級",
"type": "小雨",
"notice": "雨雖小,注意保暖別感冒"
}
更多操作請參考官方文檔:https://stedolan.github.io/jq/manual/