[Reprint] Linux command line json artifact Linux command line jq jq artifact json

Linux command line json artifact jq

 
https://www.cnblogs.com/chenqionghe/p/11736942.html

 

jq awk or a similar artifact as grep, conveniently in the command line json

Here I use Wanning weather interfaces make presentations, address: http://t.weather.sojson.com/api/weather/city/101310215

First, install

Official website: https://stedolan.github.io/jq/download/
is the basic brew install, apt install, yum install like very simple

Second, common operations

1. Format json

Direct request, significantly less legible format

➜  ~ 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 format used, as follows

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": "记得随身携带雨伞哦" },...

Already formatted

2. Take specified field

Direct entry field, use the Nested access, for example, to get information yesterday

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. Filter the specified field

Using a json specify all fields, such as {date, high}, obtaining the maximum temperature below yesterday

 curl -s http://t.weather.sojson.com/api/weather/city/101310215 | jq .data.yesterday|jq "{date,high}" { "date": "24", "high": "高温 29℃" }

4. Get the value of the plurality of fields

Use commas to get more

curl -s http://t.weather.sojson.com/api/weather/city/101310215 | jq .data.yesterday|jq ".date, .high" "24" "高温 29℃"

5. Filter Array

Directly to the specified array index

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": "雨虽小,注意保暖别感冒" }

More Actions Please refer to the official documentation: https://stedolan.github.io/jq/manual/

jq awk or a similar artifact as grep, conveniently in the command line json

Here I use Wanning weather interfaces make presentations, address: http://t.weather.sojson.com/api/weather/city/101310215

First, install

Official website: https://stedolan.github.io/jq/download/
is the basic brew install, apt install, yum install like very simple

Second, common operations

1. Format json

Direct request, significantly less legible format

➜  ~ 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 format used, as follows

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": "记得随身携带雨伞哦" },...

Already formatted

2. Take specified field

Direct entry field, use the Nested access, for example, to get information yesterday

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. Filter the specified field

Using a json specify all fields, such as {date, high}, obtaining the maximum temperature below yesterday

 curl -s http://t.weather.sojson.com/api/weather/city/101310215 | jq .data.yesterday|jq "{date,high}" { "date": "24", "high": "高温 29℃" }

4. Get the value of the plurality of fields

Use commas to get more

curl -s http://t.weather.sojson.com/api/weather/city/101310215 | jq .data.yesterday|jq ".date, .high" "24" "高温 29℃"

5. Filter Array

Directly to the specified array index

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": "雨虽小,注意保暖别感冒" }

More Actions Please refer to the official documentation: https://stedolan.github.io/jq/manual/

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/12004538.html