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. Introducing ASP.NET Core: The New ASP.NET in Town!

    The new version of ASP.NET is called ASP.NET Core (a.k.a ASP.NET 5) and it has the most significant ...

  2. Eclipse NDK 配置,不用安装Cygwin

    一.关于NDK:NDK全称:Native Development Kit.1.NDK是一系列工具的集合.NDK提供了一系列的工具,帮助开发者快速开发C(或C++)的动态库,并能自动将so和java应用 ...

  3. Appium+python自动化55-appium desktop每次启动安装Unlock和Appium Setting问题

    前言 部分真机可能会出现每次运行代码,启动app之前都会重复安装Unlock和Appium Setting这两个小工具,有的手机会自动安装,这个还好. 有的手机每次都会弹出一个安装确认框(如部分小米和 ...

  4. Matplotlib Tutorial(译)

    Matplotlib Tutorial(译) 翻译自:Matplotlib tutorialNicolas P. Rougier - Euroscipy 2012 toc{: toc} 这个教程基于可 ...

  5. 数学图形(2.8)Viviani曲线

    维维亚尼(Viviani , Vincenzo)意大利数学家.1622年4月5日生于托斯卡纳大区佛罗伦萨:1703年9月22日卒于佛罗伦萨. 这是一个圆柱与一个球相交而生成的曲线. #http://w ...

  6. WebService_java编写Webservice_Axis2_1.6

    最近给某省国家电网写一套系统,由于内部数据库单向隔离装置不支持ODBC, 原来c#写的webservice 和.net ,iis就需要换成java这一套... 下面是用Axis2 写的webservi ...

  7. windows获取本机MAC地址并写入文件的bat

    windows获取本机MAC地址并写入文件的bat MAC(Media Access Control)地址,或称为 MAC地址.硬件地址,用来定义网络设备的位置. bat代码例如以下: @echo o ...

  8. 一些C++11语言新特性 - Range-Based for Loops

    1. Range-Based for Loops for ( decl : coll ) { statement} eg: , , , , , , , } ) { std::cout << ...

  9. mysql数据库安装、启动及权限设置

    1. 安装需安装mysql客户端和服务器端. Centos下,可用命令:yum install mysql安装mysql客户端:使用命令:yum install mysql-server安装mysql ...

  10. Jquery重新学习之三[属性addClass(),removeClass(),toggleClass()]

    1:属性.addClass(class|fn)及.removeClass(class|fn) 1.1 .addClass(class) 参数class一个或多个要添加到元素中的CSS类名,请用空格分开 ...