jq神器

处理json数据

支持过滤某字段

支持数学运算(对字段处理)

  • 安装
yum install -y jq
  • 使用

参考:

http://blog.just4fun.site/command-tool-jq.html

https://www.ibm.com/developerworks/cn/linux/1612_chengg_jq/index.html

在线jq表达式匹配:

https://jqplay.org/

  • 教程

https://stedolan.github.io/jq/tutorial/

过滤超过18岁的

[{
"name" : "maotai",
"age" : 18,
"gender" : "male"
}, {
"name" : "maotai",
"age" : 19,
"gender" : "male"
},
{
"name" : "maotai",
"age" : 20,
"gender" : "male"
}]
cat t.tt | jq -r 'map(select(.age>18))'

json数据的第一项

cat t.tt | jq '.[0]'
  • 示例json数据

{
"sha": "79ece359819cdd7d033d272af9758ae22204c2ef",
"commit": {
"author": {
"name": "William Langford",
"email": "wlangfor@gmail.com",
"date": "2017-12-05T01:10:56Z"
},
"committer": {
"name": "William Langford",
"email": "wlangfor@gmail.com",
"date": "2017-12-05T01:10:56Z"
},
"message": "Fix hang for slurped inputs with trailing newline",
"tree": {
"sha": "d3b481f3448ecd50bf4aa109fd6564dd923afede",
"url": "https://api.github.com/repos/stedolan/jq/git/trees/d3b481f3448ecd50bf4aa109fd6564dd923afede"
},
"url": "https://api.github.com/repos/stedolan/jq/git/commits/79ece359819cdd7d033d272af9758ae22204c2ef",
"comment_count": 0,
"verification": {
"verified": false,
"reason": "unsigned",
"signature": null,
"payload": null
}
},
"url": "https://api.github.com/repos/stedolan/jq/commits/79ece359819cdd7d033d272af9758ae22204c2ef",
"html_url": "https://github.com/stedolan/jq/commit/79ece359819cdd7d033d272af9758ae22204c2ef",
"comments_url": "https://api.github.com/repos/stedolan/jq/commits/79ece359819cdd7d033d272af9758ae22204c2ef/comments",
"author": {
"login": "wtlangford",
"id": 3422295,
"avatar_url": "https://avatars2.githubusercontent.com/u/3422295?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/wtlangford",
"html_url": "https://github.com/wtlangford",
"followers_url": "https://api.github.com/users/wtlangford/followers",
"following_url": "https://api.github.com/users/wtlangford/following{/other_user}",
"gists_url": "https://api.github.com/users/wtlangford/gists{/gist_id}",
"starred_url": "https://api.github.com/users/wtlangford/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/wtlangford/subscriptions",
"organizations_url": "https://api.github.com/users/wtlangford/orgs",
"repos_url": "https://api.github.com/users/wtlangford/repos",
"events_url": "https://api.github.com/users/wtlangford/events{/privacy}",
"received_events_url": "https://api.github.com/users/wtlangford/received_events",
"type": "User",
"site_admin": false
},
"committer": {
"login": "wtlangford",
"id": 3422295,
"avatar_url": "https://avatars2.githubusercontent.com/u/3422295?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/wtlangford",
"html_url": "https://github.com/wtlangford",
"followers_url": "https://api.github.com/users/wtlangford/followers",
"following_url": "https://api.github.com/users/wtlangford/following{/other_user}",
"gists_url": "https://api.github.com/users/wtlangford/gists{/gist_id}",
"starred_url": "https://api.github.com/users/wtlangford/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/wtlangford/subscriptions",
"organizations_url": "https://api.github.com/users/wtlangford/orgs",
"repos_url": "https://api.github.com/users/wtlangford/repos",
"events_url": "https://api.github.com/users/wtlangford/events{/privacy}",
"received_events_url": "https://api.github.com/users/wtlangford/received_events",
"type": "User",
"site_admin": false
},
"parents": [
{
"sha": "f06deb828a318536b85d68280d429c3a70b21259",
"url": "https://api.github.com/repos/stedolan/jq/commits/f06deb828a318536b85d68280d429c3a70b21259",
"html_url": "https://github.com/stedolan/jq/commit/f06deb828a318536b85d68280d429c3a70b21259"
}
]
}
  • 格式化json
curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.'
  • 输出数组的第一项
curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.[0]'
  • 按照某些字段重组json
jq '.[0] | {message: .commit.message, name: .commit.committer.name}'
  • 按照某些字段重组json(抽出所有项)
jq '.[] | {message: .commit.message, name: .commit.committer.name}'
  • 按照某些字段重组json(抽出所有项)(结果弄成一个[])
jq '[.[] | {message: .commit.message, name: .commit.committer.name}]'
  • 返回的字段值为数组
jq '[.[] | {message: .commit.message, name: .commit.committer.name, parents: [.parents[].html_url]}]'
$ curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' |jq '[.[0] | {message: .commit.message, name: .commit.committer.name, parents: [.parents[].html_url]}]'
[
{
"message": "Fix hang for slurped inputs with trailing newline",
"name": "William Langford",
"parents": [
"https://github.com/stedolan/jq/commit/f06deb828a318536b85d68280d429c3a70b21259"
]
}
]

[svc]jq神器使用的更多相关文章

  1. 一个JSON字符串和文件处理的命令行神器jq,windows和linux都可用

    这个命令行神器的下载地址:https://stedolan.github.io/jq/# Windows和Linux版本均只有两个可执行文件,大小不过2MB多. 以Windows版本为例,介绍其用法. ...

  2. 【转帖】Linux命令行操作json神器jq

    Linux命令行操作json神器jq https://www.cnblogs.com/chenqionghe/p/11736942.html jq类似一个awk或grep一样的神器,可以方便地在命令行 ...

  3. Linux 安装json神器 jq

    wget -O jq https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 chmod +x ./jq cp jq /u ...

  4. JQ:命令行 json 解析神器 —— 命令行的Jsonview

  5. [前端神器]handlebars+require基本使用方法

    最近在某网站看到了handlebars.js,出于好奇就百度了下这是神马玩意,结果让我很是欢喜,于是就开始自学下,handlebars就几个方法,蛮简单,言归正传! 以下是基本教学逻辑演示,会附完整代 ...

  6. [前端神器]handlebars+requirejs基本使用方法

    最近在某网站看到了handlebars.js,出于好奇就百度了下这是神马玩意,结果让我很是欢喜,于是就开始自学下,handlebars就几个方法,蛮简单,言归正传! 以下是基本教学逻辑演示,会附完整代 ...

  7. Cobbler-自动化部署神器

    Cobbler-自动化部署神器 前言: 网络安装服务器套件 Cobbler(补鞋匠)从前,我们一直在做装机民工这份很有前途的职业.自打若干年前 Red Hat 推出了 Kickstart,此后我们顿觉 ...

  8. linux使用jq工具解析json

    jq类似一个awk或grep一样的神器,可以方便地在命令行操作json 这里我使用海南万宁的天气接口做演示,地址:http://t.weather.sojson.com/api/weather/cit ...

  9. jq处理JSON数据, jq Manual (development version)

    jq 允许你直接在命令行下对 JSON 进行操作,包括分片.过滤.转换等等.让我们通过几个例子来说明 jq 的功能:一.输出格式化,漂亮的打印效果如果我们用文本编辑器打开 JSON,有时候可能看起来会 ...

随机推荐

  1. C#类和结构体的异同点简单总结

    类和结构的异同点?异:  1.关键字不同 一个是class,一个是struct  2.类型不同,一个是引用类型,一个是值类型(一个堆区,一个栈区)        3.成员不同,结构体没有默认的构造函数 ...

  2. 《Hadoop应用开发技术详解》

    <Hadoop应用开发技术详解> 基本信息 作者: 刘刚 丛书名: 大数据技术丛书 出版社:机械工业出版社 ISBN:9787111452447 上架时间:2014-1-10 出版日期:2 ...

  3. 深入理解Apache Flink核心技术

    深入理解Apache Flink核心技术 2016年02月18日 17:04:03 阅读数:1936 标签: Apache-Flink数据流程序员JVM   版权声明:本文为博主原创文章,未经博主允许 ...

  4. iOS:Xcode的beta下编译低版本项目时,出现的Link错误( "_fwrite$UNIX2003", referenced from:)

    开发的项目多了,对于一个i386的错误,处理起来应该是得心应手的,可是仔细看来,跟之前遇到i386的错误还不大一样,直接搜i386是搜不到该问题解决的方法,你要是搜“Undefined symbols ...

  5. windows安装dcm4chee 出错 check file system group LOSSY_STORAGE for deletion

    错误情景: 解决方法: 更改服务的监听端口(参考DICOM:Ubuntu14环境下安装dcm4chee+oviyam2.1)

  6. swfupload js中 file 对象的属性

    name=3cc68cfc60b87e6dd6887d8a.jpg modificationdate=Wed   Apr 21 15:48:30 UTC+0800 2010 filestatus=-1 ...

  7. 设置pycharm为Eclipse快捷键

    Ctrl + O 根据name模糊查找当前文件中类.方法Alt + (向左箭头或者向右箭头) ,回退or前进到到之前查看或者编辑处Alt + (向上箭头或者向下箭头) ,将当前方法整体往下或者往上移动 ...

  8. [Grunt] Watch && grunt-contrib-watch

    Watch is an essential component of any Grunt build, and you will find yourself using it in almost ev ...

  9. HDU2089 ------不要62(数位dp)

    不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  10. log4cplus的安装与使用初步

    1. 简单介绍 log4cplus是C++编写的开源的日志系统,The purpose of this project is to port the excellentLog for Java (lo ...