[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,有时候可能看起来会 ...
随机推荐
- [Lua]mac 上安装lua
mac 安装lua google了好个看起来都不怎么好操作.这个是在命令行下操作的非常easy. curl -R -O http://www.lua.org/ftp/lua-5.2.3.tar.gz ...
- Js的在线代码编辑器:CodeMirror
github地址:https://github.com/codemirror/CodeMirror/tree/master/demo 里面包含需要的js.css文件以及大量的示例 官网:https:/ ...
- C#的四个基本技巧
1.如果可能尽量使用接口来编程 .NET框架包括类和接口,在编写程序的时候,你可能知道正在用.NET的哪个类.然而,在这种情况下如果你用.NET支持的接口而不是它的类来编程时,代码会变得更加稳定.可用 ...
- 通过Spring配置文件中bean中的property赋值
基本数据类型赋值-通过spring配置文件中bean中的property 扩展-以此方式可以通过配置为连接数据的属性赋值 1.如果是基本数据类型,可以通过setter方法为对象中的属性设置初始值,应用 ...
- Python基础(12)--模块
本文地址:http://www.cnblogs.com/archimedes/p/python-modules.html,转载请注明源地址. 模块简介 如果你退出 Python 解释器重新进入,以前创 ...
- 红帽RHOP 8 发布一条龙方案
导读 日前,Canonical的Ubuntu在OpenStack的云系统方面处于业界领先地位.其他诸如IBM类顶级科技公司也有意加入OpenStack的混战,新的专用OpenStack公司(例如Mir ...
- (转)scala中map与flatMap浅析
在函数式语言中,函数作为一等公民,可以在任何地方定义,在函数内或函数外,可以作为函数的参数和返回值,可以对函数进行组合.由于命令式编程语言也可以通过类似函数指针的方式来实现高阶函数,函数式的最主要的好 ...
- 吐槽win7
在纠结了N次后,终于重装win7了.其间还是不死心,又找了新的xp版本来装了一下,确实不行,才心不甘情不愿地再次回到win7下. 说实话,Win7的各种新功能,我没有感觉到有多好,只有不适应,如: 资 ...
- 【canvas】四角光阑
代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type ...
- DIV CSS布局中绝对定位和浮动用法
转自:http://developer.51cto.com/art/201009/223337_1.htm 你对DIV CSS布局中绝对定位和浮动的概念及使用是否熟悉,这里和大家分享一下,CSS中,实 ...