Shell脚本处理JSON数据工具jq
shell脚本如何方便地处理JSON格式的数据呢,这里介绍一个工具:jq
使用参数介绍:https://stedolan.github.io/jq/manual/
官方教程简单翻译如下。
1、获取JSON数据
我们以github上jq项目最新5条评论的JSON数据为例。获取数据如下:
curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5'
结果如下:
[
{
"sha": "d25341478381063d1c76e81b3a52e0592a7c997f",
"commit": {
"author": {
"name": "Stephen Dolan",
"email": "mu@netsoc.tcd.ie",
"date": "2013-06-22T16:30:59Z"
},
"committer": {
"name": "Stephen Dolan",
"email": "mu@netsoc.tcd.ie",
"date": "2013-06-22T16:30:59Z"
},
"message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161",
"tree": {
"sha": "6ab697a8dfb5a96e124666bf6d6213822599fb40",
"url": "https://api.github.com/repos/stedolan/jq/git/trees/6ab697a8dfb5a96e124666bf6d6213822599fb40"
},
"url": "https://api.github.com/repos/stedolan/jq/git/commits/d25341478381063d1c76e81b3a52e0592a7c997f",
"comment_count": 0
},
"url": "https://api.github.com/repos/stedolan/jq/commits/d25341478381063d1c76e81b3a52e0592a7c997f",
"html_url": "https://github.com/stedolan/jq/commit/d25341478381063d1c76e81b3a52e0592a7c997f",
"comments_url": "https://api.github.com/repos/stedolan/jq/commits/d25341478381063d1c76e81b3a52e0592a7c997f/comments",
"author": {
"login": "stedolan",
...
2、高亮并按属性排序显示
用 jq '.'即可:
curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.'
结果:
[
{
"parents": [
{
"html_url": "https://github.com/stedolan/jq/commit/54b9c9bdb225af5d886466d72f47eafc51acb4f7",
"url": "https://api.github.com/repos/stedolan/jq/commits/54b9c9bdb225af5d886466d72f47eafc51acb4f7",
"sha": "54b9c9bdb225af5d886466d72f47eafc51acb4f7"
},
{
"html_url": "https://github.com/stedolan/jq/commit/8b1b503609c161fea4b003a7179b3fbb2dd4345a",
"url": "https://api.github.com/repos/stedolan/jq/commits/8b1b503609c161fea4b003a7179b3fbb2dd4345a",
"sha": "8b1b503609c161fea4b003a7179b3fbb2dd4345a"
}
],
"committer": {
"type": "User",
"received_events_url": "https://api.github.com/users/stedolan/received_events",
"events_url": "https://api.github.com/users/stedolan/events{/privacy}",
"repos_url": "https://api.github.com/users/stedolan/repos",
"organizations_url": "https://api.github.com/users/stedolan/orgs",
...
这里的评论内容比较多,我们现在想拿第一个评论。
3、获取数组某一项
curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.[0]'
结果:
可以看到,我们已经拿到了一条完整的评论内容。但我们真正关心的只是评论内容和用户名,下面来获取这两项内容。
4、自定义格式输出某一项
jq '.[0] | {message: .commit.message, name: .commit.committer.name}'
结果:
{
"name": "Stephen Dolan",
"message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161"
}
我们可以看到,已经拿到了想要的内容,并且已经按我们自己定义的格式显示了。
这里 | 后面的内容是以前面的内容为输入的,.commit 中的 . 就是指 .[0] 中的内容。
5、自定义格式输出多项
jq '.[] | {message: .commit.message, name: .commit.committer.name}'
结果:
{
"name": "Stephen Dolan",
"message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161"
}
{
"name": "Stephen Dolan",
"message": "Reject all overlong UTF8 sequences."
}
{
"name": "Stephen Dolan",
"message": "Fix various UTF8 parsing bugs.\n\nIn particular, parse bad UTF8 by replacing the broken bits with U+FFFD\nand resychronise correctly after broken sequences."
}
{
"name": "Stephen Dolan",
"message": "Fix example in manual for `floor`. See #155."
}
{
"name": "Nicolas Williams",
"message": "Document floor"
}
这里 .[] 获取的是数组中的所有项。
我们看到,结果是一个个独立的JSON对象,如何把结果组合成一个数组呢?
6、以数组形式自定义输出多项
jq '[.[] | {message: .commit.message, name: .commit.committer.name}]'
结果:
[
{
"name": "Stephen Dolan",
"message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161"
},
{
"name": "Stephen Dolan",
"message": "Reject all overlong UTF8 sequences."
},
{
"name": "Stephen Dolan",
"message": "Fix various UTF8 parsing bugs.\n\nIn particular, parse bad UTF8 by replacing the broken bits with U+FFFD\nand resychronise correctly after broken sequences."
},
{
"name": "Stephen Dolan",
"message": "Fix example in manual for `floor`. See #155."
},
{
"name": "Nicolas Williams",
"message": "Document floor"
}
]
我们可以看到,只要在上一步的命令中内容的两端加个中括号即可。
最后,我们如果想获取每个评论的引用评论的url(在parents节点中,有一个或多个)呢?
7、获取其他内容
jq '[.[] | {message: .commit.message, name: .commit.committer.name, parents: [.parents[].html_url]}]'
结果:
[
{
"parents": [
"https://github.com/stedolan/jq/commit/54b9c9bdb225af5d886466d72f47eafc51acb4f7",
"https://github.com/stedolan/jq/commit/8b1b503609c161fea4b003a7179b3fbb2dd4345a"
],
"name": "Stephen Dolan",
"message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161"
},
{
"parents": [
"https://github.com/stedolan/jq/commit/ff48bd6ec538b01d1057be8e93b94eef6914e9ef"
],
"name": "Stephen Dolan",
"message": "Reject all overlong UTF8 sequences."
},
{
"parents": [
"https://github.com/stedolan/jq/commit/54b9c9bdb225af5d886466d72f47eafc51acb4f7"
],
"name": "Stephen Dolan",
"message": "Fix various UTF8 parsing bugs.\n\nIn particular, parse bad UTF8 by replacing the broken bits with U+FFFD\nand resychronise correctly after broken sequences."
},
{
"parents": [
"https://github.com/stedolan/jq/commit/3dcdc582ea993afea3f5503a78a77675967ecdfa"
],
"name": "Stephen Dolan",
"message": "Fix example in manual for `floor`. See #155."
},
{
"parents": [
"https://github.com/stedolan/jq/commit/7c4171d414f647ab08bcd20c76a4d8ed68d9c602"
],
"name": "Nicolas Williams",
"message": "Document floor"
}
]
这里用 .parents[].html_url 获取当前项的 parents 节点中的所有项的 html_url 属性的内容,然后两边加个中括号组装成数组输出。
怎么样,经过这个例子可以看出,用jq处理JSON数据还是很方便强大的吧。
8、注意点
jq能处理的需要是严格的JSON格式数据,JSON对象和JSON字符串是不行的,如下面的两种格式数据jq是不能处理的:
json对象:
{
a: 1,
b: {
c: "abc"
}
}
json字符串:
'{"a":1,"b":{"c":"abc"}}'
正确的JSON格式:
{
"a": 1,
"b": {
"c": "abc"
}
}
关于什么是JSON格式数据,请参考:http://www.json.org/
参考文档:https://www.jianshu.com/p/6de3cfdbdb0e
Shell脚本处理JSON数据工具jq的更多相关文章
- Shell学习---Shell脚本的静态检查工具shellcheck
Shell脚本的静态检查工具shellcheck ubuntu下 apt install shellcheck ,即可安装shellcheck.写完shell脚本,记得用它检查一下,能给你点建议的.要 ...
- shell编程系列24--shell操作数据库实战之利用shell脚本将文本数据导入到mysql中
shell编程系列24--shell操作数据库实战之利用shell脚本将文本数据导入到mysql中 利用shell脚本将文本数据导入到mysql中 需求1:处理文本中的数据,将文本中的数据插入到mys ...
- 用 shell 脚本做命令行工具扩展
问题的提出 公司开发机与远程服务器之间有严格的隔离策略,不能直接使用 ssh 登录,而必需通过跳板机.这样一来,本地与服务器之间的一些文件传输变得非常不便.经过咨询,运维教了我一招: $ nc -l ...
- shell脚本中的数据传递方式
shell中支持的数据传递方式 主要有那么几种: 变量.管道.结果引用.重定向+文件.以及xargs. 变量方式: 1. 定义变量: 变量名=值 2. 使用变量: $变量名 管道方式: 统计当前文件夹 ...
- 案例:通过shell脚本实现mysql数据备份与清理
Shell是系统的用户界面,提供了用户与内核进行交互操作的一种接口.它接收用户输入的命令并把它送入内核去执行,实际上Shell是一个命令解释器,它解释由用户输入的命令并且把它们送到内核,不仅如此,Sh ...
- shell脚本处理二进制数据
正确处理二进制数据 正确处理二进制数据必须保证以下三个环节是二进制安全(Binary Safe)的: 从文件读取至内存: 处理数据过程中: 内存写入至文件. 那么二进制安全是什么?通俗来说就是不会特殊 ...
- JSONProxy - 获取跨域json数据工具
JSONProxy是一款很好的获取json数据的代理网站,“Enables cross-domain requests to any JSON API”.当你苦于无法跨域获取json数据时,不妨一试, ...
- shell脚本处理大数据系列之(一)方法小结
转自:http://longriver.me/?p=57 方法1: 单进程处理大规模的文件速度如(上million量级)比较慢,可以采用awk取模的方法,将文件分而治之,这样可以利用充分的利用多核CP ...
- Shell脚本实现用户数据导入
#输入:固定格式的用户数据user.sql #处理:循环读取user.sql中的每行(每行对应一条用户数据),依次调用curl命令将用户插入BearyChat #输出:执行结果输出到日志文件outlo ...
随机推荐
- vue Axios 封装与配置项
import axios from "axios"; import qs from "qs"; import { Message } from "el ...
- Django runserver UnicodeDecodeError
编码问题可以说是我遇到过的python 2.7最大的败笔 今天写django时,很简单的一个项目却报UnicodeDecodeError,而我的代码中一个中文字符都没有出现. 如下: 网上找到的所谓解 ...
- 剑指Offer 13. 调整数组顺序使奇数位于偶数前面 (数组)
题目描述 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. 题目地址 https ...
- Linuxs升级系统自带的openssh
最近有空复习了一下Linux,在虚拟机上安装了个CentOS6.5,顺便升级一下系统自带的openssh,任何系统操作都有风险,正式环境请做好备份工作.废话少说,直接贴代码. 1.准备工作 ...
- 内存泄露java.lang.OutOfMemoryError: PermGen space解决方法
PermGen space的全称是Permanent Generation space,是指内存的永久保存区域,这块内存主要是被JVM存放Class和Meta信息的,Class在被Loader时就会被 ...
- c++ 11 移动语义
C++ 已经拥有了拷贝构造函数, 和赋值函数,它们主要定位为浅和深度拷贝, 新增加一个移动构造函数,主要避免拷贝构造. 在定义了移动构造函数的情况下,在实参(argument)是一个右值(rvalue ...
- quartz Cron表达式解读
CronTrigger CronTriggers往往比SimpleTrigger更有用,如果您需要基于日历的概念,而非SimpleTrigger完全指定的时间间隔,复发的发射工作的时间表. CronT ...
- Gym101889B. Buggy ICPC(打表)
比赛链接:传送门 题目: Problem B – Buggy ICPC Author : Gabriel Poesia, Brasil Alan Curing is a famous sports p ...
- oracle query
不等值连接查询 员工工资级别 select e.empno,e.ename,e.sal,s.grade from emp e,salgrade s where e.sal between s.losa ...
- 《Linux内核原理与分析》第四周作业
课本:第3章 MenuOS的构造 内容总结 计算机的"三大法宝" 存储程序计算机 函数调用堆栈 中断 操作系统的"两把宝剑" 中断上下文切换:保存现场和恢复现场 ...