Linux命令行操作json神器jq

前言

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/

posted @ 2019-10-25 14:15  雪山飞猪  阅读(4702)  评论(0编辑  收藏  举报