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. ~ curl -s http://t.weather.sojson.com/api/weather/city/101310215
  2. {"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格式化,如下

  1. curl -s http://t.weather.sojson.com/api/weather/city/101310215 | jq
  2. {
  3. "message": "success感谢又拍云(upyun.com)提供CDN赞助",
  4. "status": 200,
  5. "date": "20191025",
  6. "time": "2019-10-25 10:27:28",
  7. "cityInfo": {
  8. "city": "万宁市",
  9. "citykey": "101310215",
  10. "parent": "海南",
  11. "updateTime": "09:59"
  12. },
  13. "data": {
  14. "shidu": "92%",
  15. "pm25": 20,
  16. "pm10": 49,
  17. "quality": "优",
  18. "wendu": "25",
  19. "ganmao": "各类人群可自由活动",
  20. "forecast": [
  21. {
  22. "date": "25",
  23. "high": "高温 29℃",
  24. "low": "低温 23℃",
  25. "ymd": "2019-10-25",
  26. "week": "星期五",
  27. "sunrise": "06:35",
  28. "sunset": "18:10",
  29. "aqi": 44,
  30. "fx": "无持续风向",
  31. "fl": "<3级",
  32. "type": "中雨",
  33. "notice": "记得随身携带雨伞哦"
  34. },...

已经格式化过

直接输入字段,使用.嵌套访问,如要获取昨天的信息

  1. curl -s http://t.weather.sojson.com/api/weather/city/101310215 | jq .data.yesterday
  2. {
  3. "date": "24",
  4. "high": "高温 29℃",
  5. "low": "低温 23℃",
  6. "ymd": "2019-10-24",
  7. "week": "星期四",
  8. "sunrise": "06:34",
  9. "sunset": "18:10",
  10. "aqi": 27,
  11. "fx": "无持续风向",
  12. "fl": "<3级",
  13. "type": "多云",
  14. "notice": "阴晴之间,谨防紫外线侵扰"
  15. }

使用一个json指定所有字段,如{date, high},获取昨天的最高温度如下

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

使用逗号获取多个

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

直接指定数组的索引即可

  1. curl -s http://t.weather.sojson.com/api/weather/city/101310215 |jq ".data.forecast"|jq ".[0,9]"
  2. {
  3. "date": "25",
  4. "high": "高温 29℃",
  5. "low": "低温 23℃",
  6. "ymd": "2019-10-25",
  7. "week": "星期五",
  8. "sunrise": "06:35",
  9. "sunset": "18:10",
  10. "aqi": 44,
  11. "fx": "无持续风向",
  12. "fl": "<3级",
  13. "type": "中雨",
  14. "notice": "记得随身携带雨伞哦"
  15. }
  16. {
  17. "date": "03",
  18. "high": "高温 26℃",
  19. "low": "低温 24℃",
  20. "ymd": "2019-11-03",
  21. "week": "星期日",
  22. "sunrise": "06:38",
  23. "sunset": "18:05",
  24. "fx": "东风",
  25. "fl": "3-4级",
  26. "type": "小雨",
  27. "notice": "雨虽小,注意保暖别感冒"
  28. }

更多操作请参考官方文档:https://stedolan.github.io/jq/manual/

版权声明:本文为chenqionghe原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/chenqionghe/p/11736942.html