当前位置:首页 » JavaScript技术

命令行下JSON处理工具:jq

2016-02-02 23:43 本站整理 浏览(12)

1.背景

接口返回的数据类型基本是JSON, 在命令行获取到接口返回的数据,往往是一大堆数据,怎样在一大堆数据中只想获取到我想要的数据,就需要一个特定的工具:jq,格式化输出,方便校验。

2.jq是什么

jq是轻量级和便捷的命令行JSON解析器。
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.

3.install

linux && OS X
[code]git clonehttps://github.com/stedolan/jq.git cd jq
autoreconf -i
./configure --disable-maintainer-mode
make
sudo make install

4.常用方法

格式化:.
[code]hugangdeMacBook-Pro:~ root# curl 'http://api.map.baidu.com/telematics/v3/weather?location=shanghai&output=json&ak=W69oaDTCfuGwzNwmtVvgWfGH' -s | jq '.'
{
  "error": 0,
  "status": "success",
  "date": "2016-02-02",
  "results": [
    {
      "currentCity": "shanghai",
      "pm25": "170",
      "index": [
        {
          "title": "穿衣",
          "zs": "较冷",
          "tipt": "穿衣指数",
          "des": "建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。"
        },
        {
          "title": "洗车",
          "zs": "较适宜",
          "tipt": "洗车指数",
          "des": "较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"
        },
        {
          "title": "旅游",
          "zs": "适宜",
          "tipt": "旅游指数",
          "des": "天气较好,气温稍低,会感觉稍微有点凉,不过也是个好天气哦。适宜旅游,可不要错过机会呦!"
        },
        {
          "title": "感冒",
          "zs": "易发",
          "tipt": "感冒指数",
          "des": "昼夜温差很大,易发生感冒,请注意适当增减衣服,加强自我防护避免感冒。"
        },
        {
          "title": "运动",
          "zs": "较适宜",
          "tipt": "运动指数",
          "des": "天气较好,无雨水困扰,较适宜进行各种运动,但因气温较低,在户外运动请注意增减衣物。"
        },
        {
          "title": "紫外线强度",
          "zs": "弱",
          "tipt": "紫外线强度指数",
          "des": "紫外线强度较弱,建议出门前涂擦SPF在12-15之间、PA+的防晒护肤品。"
        }
      ],
      "weather_data": [
        {
          "date": "周二 02月02日 (实时:3℃)",
          "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/qing.png",
          "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/qing.png",
          "weather": "晴",
          "wind": "微风",
          "temperature": "5 ~ 0℃"
        },
        {
          "date": "周三",
          "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/qing.png",
          "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/duoyun.png",
          "weather": "晴转多云",
          "wind": "微风",
          "temperature": "7 ~ 2℃"
        },
        {
          "date": "周四",
          "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/xiaoyu.png",
          "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/yin.png",
          "weather": "小雨转阴",
          "wind": "东风微风",
          "temperature": "6 ~ 2℃"
        },
        {
          "date": "周五",
          "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/duoyun.png",
          "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/qing.png",
          "weather": "多云转晴",
          "wind": "北风微风",
          "temperature": "9 ~ 1℃"
        }
      ]
    }
  ]
}

获取特定字段:.特定字段
[code]curl 'http://api.map.baidu.com/telematics/v3/weather?location=shanghai&output=json&ak=W69oaDTCfuGwzNwmtVvgWfGH' -s | jq '.status'
"success"

数组操作
数组中第一个元素
[code]hugangdeMacBook-Pro:~ hugang$ curl 'http://api.map.baidu.com/telematics/v3/weather?location=shanghai&output=json&ak=W69oaDTCfuGwzNwmtVvgWfGH' -s | jq '.results[0].currentCity'
"shanghai"

重新组合
.results[]
:返回results数组所有数据;
|
:the output of one filter into the input of another,类似管道符;{biaoti: .title, miaoshu: .des} 将input中.title,.des做为biaoti,miaoshu的value。
[code]hugangdeMacBook-Pro:~ hugang$ curl 'http://api.map.baidu.com/telematics/v3/weather?location=shanghai&output=json&ak=W69oaDTCfuGwzNwmtVvgWfGH' -s | jq '.results[] | .index[] | {biaoti: .title, miaoshu: .des}'
{
  "biaoti": "穿衣",
  "miaoshu": "建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。"
}
{
  "biaoti": "洗车",
  "miaoshu": "较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"
}
{
  "biaoti": "旅游",
  "miaoshu": "天气较好,气温稍低,会感觉稍微有点凉,不过也是个好天气哦。适宜旅游,可不要错过机会呦!"
}
{
  "biaoti": "感冒",
  "miaoshu": "昼夜温差很大,易发生感冒,请注意适当增减衣服,加强自我防护避免感冒。"
}
{
  "biaoti": "运动",
  "miaoshu": "天气较好,无雨水困扰,较适宜进行各种运动,但因气温较低,在户外运动请注意增减衣物。"
}
{
  "biaoti": "紫外线强度",
  "miaoshu": "紫外线强度较弱,建议出门前涂擦SPF在12-15之间、PA+的防晒护肤品。"
}

获取多个结果
,
分隔多个结果
[code]curl 'http://api.map.baidu.com/telematics/v3/weather?location=shanghai&output=json&ak=W69oaDTCfuGwzNwmtVvgWfGH' -s | jq '.error, .date'
0
"2016-02-02"

长度
length 根据input判断长度,字符串,数组,对象的长度
JSON对象:
[code]hugangdeMacBook-Pro:~ hugang$ curl 'http://api.map.baidu.com/telematics/v3/weather?location=shanghai&output=json&ak=W69oaDTCfuGwzNwmtVvgWfGH' -s | jq '.| length'
4

JSON数组
[code]curl 'http://api.map.baidu.com/telematics/v3/weather?location=shanghai&output=json&ak=W69oaDTCfuGwzNwmtVvgWfGH' -s | jq '.results[] | .index | length'
6

字符串
[code]curl 'http://api.map.baidu.com/telematics/v3/weather?location=shanghai&output=json&ak=W69oaDTCfuGwzNwmtVvgWfGH' -s | jq '.status | length'
7

keys
[code]curl 'http://api.map.baidu.com/telematics/v3/weather?location=shanghai&output=json&ak=W69oaDTCfuGwzNwmtVvgWfGH' -s | jq '. | keys' 
[
  "date",
  "error",
  "results",
  "status"
]

-S 根据key排序输出
[code]curl 'http://api.map.baidu.com/telematics/v3/weather?location=shanghai&output=json&ak=W69oaDTCfuGwzNwmtVvgWfGH' -s | jq -S 'keys'
[
  "date",
  "error",
  "results",
  "status"
]

map(foo)
invoke filter foo for each input
[code]echo '[1,5,3,0,7]' | jq 'map(.+11)'
[
  12,
  16,
  14,
  11,
  18
]

select(foo)
返回满足条件的数据
[code]echo '[1,5,3,0,7]' | jq 'map(select(.<2))'
[
  1,
  0
]

if-then-else-end
条件判断
[code]curl 'http://api.map.baidu.com/telematics/v3/weather?location=shanghai&output=json&ak=W69oaDTCfuGwzNwmtVvgWfGH' -s | jq 'if .error==0 then "ok" elif .==1 then "false" else "null" end'
"ok"