[svc]jq神器使用
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神器使用的更多相关文章
- 一个JSON字符串和文件处理的命令行神器jq,windows和linux都可用
这个命令行神器的下载地址:https://stedolan.github.io/jq/# Windows和Linux版本均只有两个可执行文件,大小不过2MB多. 以Windows版本为例,介绍其用法. ...
- 【转帖】Linux命令行操作json神器jq
Linux命令行操作json神器jq https://www.cnblogs.com/chenqionghe/p/11736942.html jq类似一个awk或grep一样的神器,可以方便地在命令行 ...
- Linux 安装json神器 jq
wget -O jq https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 chmod +x ./jq cp jq /u ...
- JQ:命令行 json 解析神器 —— 命令行的Jsonview
- [前端神器]handlebars+require基本使用方法
最近在某网站看到了handlebars.js,出于好奇就百度了下这是神马玩意,结果让我很是欢喜,于是就开始自学下,handlebars就几个方法,蛮简单,言归正传! 以下是基本教学逻辑演示,会附完整代 ...
- [前端神器]handlebars+requirejs基本使用方法
最近在某网站看到了handlebars.js,出于好奇就百度了下这是神马玩意,结果让我很是欢喜,于是就开始自学下,handlebars就几个方法,蛮简单,言归正传! 以下是基本教学逻辑演示,会附完整代 ...
- Cobbler-自动化部署神器
Cobbler-自动化部署神器 前言: 网络安装服务器套件 Cobbler(补鞋匠)从前,我们一直在做装机民工这份很有前途的职业.自打若干年前 Red Hat 推出了 Kickstart,此后我们顿觉 ...
- linux使用jq工具解析json
jq类似一个awk或grep一样的神器,可以方便地在命令行操作json 这里我使用海南万宁的天气接口做演示,地址:http://t.weather.sojson.com/api/weather/cit ...
- jq处理JSON数据, jq Manual (development version)
jq 允许你直接在命令行下对 JSON 进行操作,包括分片.过滤.转换等等.让我们通过几个例子来说明 jq 的功能:一.输出格式化,漂亮的打印效果如果我们用文本编辑器打开 JSON,有时候可能看起来会 ...
随机推荐
- SharePoint 入门书籍推荐
最近,总有人说刚入门SharePoint,没有好的资料或者电子书,资料推荐大家多看看博客园和CSDN的博客.对于看博客,我一般是两个思路,要么找一个人的从头到尾看一遍,觉得有意义的,就把地址加收藏:或 ...
- GO -- 一个经验
加锁要在有用的的上下文再加锁, 不要加的范围多了, 否则被锁住.
- Win7下安装pip
1.首先下载setuptools,下载地址https://pypi.python.org/pypi/setuptools#downloads2.解压下载后的文件,进入命令行,将目录切换到解压后文件夹所 ...
- boa cgi程序cgi_header: unable to find LFLF
ftp必须用二进制模式上传才可以 sqlite3 arm-linux-gcc hello.c -o hello.cgi -I /cgi/include -L /cgi/lib -static -lsq ...
- Android(安卓)开发通过NDK调用JNI,使用opencv做本地c++代码开发配置方法 边缘检测 范例代码
以前写过两个Android开发配置文档,使用NDK进行JNI开发,这样能够利用以前已经写好的C++代码. 前两篇博客地址: http://blog.csdn.net/watkinsong/articl ...
- Python学习(四)数据结构 —— int float
Python 数字类型 int float 数字常量 int: 一般的整数, long: 长整型,2.x版本需在数字后加 “L” 或 “l” ,表示长整型 如 100000000L: python ...
- T-sql 根据bak文件恢复新建数据库
利用bak文件恢复新建数据库: 1:利用sqlserver界面管理工具恢复,在操作2005以上的版本可以讲界面的操作过程生成sql语句(本人在此徘徊了好久,得一位博友提醒才恍然大悟); 2:利用sql ...
- 查看和调试Qt源码(动态编译的QT也可进入源码)good
简述 在调试程序的时候,有时需要调试进入 Qt 源码,这不仅有利于我们了解内部实现机制,而且对于解决一些隐蔽性问题很有帮助. 都知道 F11 是“单步进入”,可是在调试的过程中,按下 F11 却无法进 ...
- hello--GAN
GAN系列学习(1)——前生今世 DCGAN.WGAN.WGAN-GP.LSGAN.BEGAN原理总结及对比 [Learning Notes]变分自编码器(Variational Auto-Encod ...
- event & EventHandler
[event & EventHandler] 在老C#中EventHandler指的是一个需要定义一个delegate,这个delegate是回调的规范.例如: public delegate ...