命令行下JSON处理工具:jq
2016-02-02 23:43
766 查看
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 Xgit clone https://github.com/stedolan/jq.git cd jq autoreconf -i ./configure --disable-maintainer-mode make sudo make install
4.常用方法
格式化:.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℃" } ] } ] }
获取特定字段:.特定字段
curl 'http://api.map.baidu.com/telematics/v3/weather?location=shanghai&output=json&ak=W69oaDTCfuGwzNwmtVvgWfGH' -s | jq '.status' "success"
数组操作
数组中第一个元素
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。
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+的防晒护肤品。" }
获取多个结果
,分隔多个结果
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对象:
hugangdeMacBook-Pro:~ hugang$ curl 'http://api.map.baidu.com/telematics/v3/weather?location=shanghai&output=json&ak=W69oaDTCfuGwzNwmtVvgWfGH' -s | jq '.| length' 4
JSON数组
curl 'http://api.map.baidu.com/telematics/v3/weather?location=shanghai&output=json&ak=W69oaDTCfuGwzNwmtVvgWfGH' -s | jq '.results[] | .index | length' 6
字符串
curl 'http://api.map.baidu.com/telematics/v3/weather?location=shanghai&output=json&ak=W69oaDTCfuGwzNwmtVvgWfGH' -s | jq '.status | length' 7
keys
curl 'http://api.map.baidu.com/telematics/v3/weather?location=shanghai&output=json&ak=W69oaDTCfuGwzNwmtVvgWfGH' -s | jq '. | keys' [ "date", "error", "results", "status" ]
-S 根据key排序输出
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
echo '[1,5,3,0,7]' | jq 'map(.+11)' [ 12, 16, 14, 11, 18 ]
select(foo)
返回满足条件的数据
echo '[1,5,3,0,7]' | jq 'map(select(.<2))' [ 1, 0 ]
if-then-else-end
条件判断
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"
相关文章推荐
- Gulp.js深入讲解
- js引擎garbage collection的问题
- JavaScript模块化与esl.js
- Dom及JavaScript图片库
- 个人笔记 js 15 js字符串的转换
- 个人笔记 js 14 js实现点击单元格行跳转链接到指定页面
- extjs查找表单中的文本框值
- 个人笔记 js 13 点击事件触发弹窗询问选项
- [转]FastJSON通过SerializeFilter定制序列化
- #学习笔记#(30)牛客网JS测试题21~45
- js调试工具Console命令详解
- 如何实现JSP网页模板 JSP网页母版
- javascript异步过程
- BZOJ 1013: [JSOI2008]球形空间产生器sphere
- JSTL不同版本和EL表达式的关联
- 如何在JavaScript里取session的值
- 什么是javaScript闭包
- JavaScript基础笔记集合
- 立即执行函数表达式 项目经常用到js 代码Module模式
- 不学就吃亏的underscorejs类库学习示例 ——(集合篇)