[Linux] jq:命令行JSON处理工具
jq命令帮助我们很方便地在终端查看和处理json文件
jq命令的帮助信息:
abby@abby:bgs$ jq -h
jq - commandline JSON processor [version 1.5--a5b5cbe]
Usage: jq [options] <jq filter> [file...] jq is a tool for processing JSON inputs, applying the
given filter to its JSON text inputs and producing the
filter's results as JSON on standard output.
The simplest filter is ., which is the identity filter,
copying jq's input to its output unmodified (except for
formatting).
For more advanced filters see the jq() manpage ("man jq")
and/or https://stedolan.github.io/jq Some of the options include:
-c compact instead of pretty-printed output;
-n use `null` as the single input value;
-e set the exit status code based on the output;
-s read (slurp) all inputs into an array; apply filter to it;
-r output raw strings, not JSON texts;
-R read raw strings, not JSON texts;
-C colorize JSON;
-M monochrome (don't colorize JSON);
-S sort keys of objects on output;
--tab use tabs for indentation;
--arg a v set variable $a to value <v>;
--argjson a v set variable $a to JSON value <v>;
--slurpfile a f set variable $a to an array of JSON texts read from <f>;
See the manpage for more options.
#默认情况下,jq会将json格式化为多行树状结构输出,但有时需要将一个json串在一行输出,可使用-c参数,例如
jq -c . xxx.json
#用逗号分隔可以同时获取json中多个key的值。但过滤出的多个值会分多行显示。要注意除了逗号之外不能有其他字符(包括空格),例如
js .field1,.field2 xxx.json
使用:
$ jq . xxx.json # 直接将文件名传给 jq
$ cat bgs_enterprise_info_norm.json | jq . #或者由其他命令读出文件内容,并传给 jq
例子:
原始数据:
abby@abby:bgs$ head -n2 bgs_enterprise_info_norm.json
{"industry": "纺织服装、服饰业", "name": "xxx公司", "business": "xxxxx", "description": "空值"}
{"industry": "批发业", "name": "xxx公司", "business": "xxxxx", "description": "空值"}
格式化:
abby@abby:bgs$ head -n2 bgs_enterprise_info_norm.json | jq .
{
"industry": "纺织服装、服饰业",
"name": "xxx公司",
"business": "xxxxx",
"description": "空值"
}
{
"industry": "批发业",
"name": "xxx公司",
"business": "xxxxx",
"description": "空值"
}
选取某属性:
abby@abby:bgs$ head -n2 bgs_enterprise_info_norm.json | jq .industry
"纺织服装、服饰业"
"批发业"
abby@abby:bgs$ jq .description bgs_enterprise_info_norm.json | grep 空值 | wc -l
内建函数
jq还有一些内建函数如 key,has
key是用来获取JSON中的key元素的:
cat json_raw.txt | jq 'keys'
[
"employees",
"location",
"name"
]
has是用来是判断是否存在某个key:
cat json_raw.txt | jq 'has("name")'
true
cat json_raw.txt | jq 'has("noexisted")'
false
参考:
jq : Linux下json的命令行工具-Bean_lee-ChinaUnix博客
[Linux] jq:命令行JSON处理工具的更多相关文章
- Linux中命令行终端切换工具screen
screen命令 本文转自:http://man.linuxde.net/screen Screen是一款由GNU计划开发的用于命令行终端切换的自由软件.用户可以通过该软件同时连接多个本地或远程的命令 ...
- VMware10中的CentOS6.5命令行安装VMwaretools工具启用windows与虚拟机中Linux系统的共享目录
VMware10中的CentOS6.5命令行安装VMwaretools工具启用windows与虚拟机中Linux系统的共享目录 一.描述 系统描述:win7旗舰版64位系统+VMware Workst ...
- 使用Linux的命令行工具做简单的文本分析
Basic Text Analysis with Command Line Tools in Linux | William J Turkel 这篇文章非常清楚的介绍了如何使用Linux的命令行工具进 ...
- Linux系统——28个命令行下的工具
Unix/Linux下的28个命令行下的工具 下面是Kristóf Kovács收集的28个Unix/Linux下的28个命令行下的工具(原文链接),有一些是大家熟悉的,有一些是非常有用的,有一些是不 ...
- 28个Unix/Linux的命令行神器_转
28个Unix/Linux的命令行神器 下面是Kristóf Kovács收集的28个Unix/Linux下的28个命令行下的工具,有一些是大家熟悉的,有一些是非常有用的,有一些是不为人知的.这些工具 ...
- APICloud提供适用于命令行的开发工具,开发更具极客精神!
APICloud近期大动作不断,上周刚刚支持Atom编辑器,并推出核心开发工具库.本周又为开发者提供了一款超轻便的新开发工具--CLI工具! 操作系统: Mac/Windows/Linux nodej ...
- 28个Unix/Linux的命令行神器
下面是Kristóf Kovács收集的28个Unix/Linux下的28个命令行下的工具(原文链接),有一些是大家熟悉的,有一些是非常有用的,有一些是不为人知的.这些工具都非常不错,希望每个人都知道 ...
- 28 个 Unix/Linux 的命令行神器
28 个 Unix/Linux 的命令行神器 下面是Kristóf Kovács收集的28个Unix/Linux下的28个命令行下的工具(原文链接),有一些是大家熟悉的,有一些是非常有用的,有一些 ...
- php 运行linux命令 与 linux下命令行执行php
1.php运行linux命令 exec函数:string exec(string command, string [array], int [return_var]); 执行函数后不输出结果,返回最 ...
随机推荐
- zabbix分布式监控系统安装配置
zabbix简介: zabbix(音同 zæbix)是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵 ...
- HTML中的figure和gigcaption标签
参考自:anti-time的博客http://www.cnblogs.com/morning0529/p/4198494.html 在写xhtml.html中常常用到一种图片列表,图片+标题或者图片+ ...
- log4j:ERROR A "org.apache.log4j.DailyRollingFileAppender" object is not assignable to a "org.apache.log4j.Appender" variable.
多个classloader加载log4j时需要设置当前Thread的classloader为你自己的classloader Thread.currentThread().setContextClass ...
- Vue中使用百度地图——设置地图标注
知识点:创建Map实例,为指定的位置设置标注 参考博客:https://www.cnblogs.com/liuswi/p/3994757.html 1.效果图:初始化地图,设置指定经纬度为地图中心点坐 ...
- 02_Storm集群部署
1. 部署前的硬件及软件检查 硬件要求 1)storm集群部署包括zookeeper部署,而zookeeper集群最小为3台机器2)storm的计算过程都在内存中完成,因此内存要尽量大3)storm少 ...
- js实现全选checkbox
js代码 function selectAllCheckBox(parentid) { var PID = document.getElementById(parentid); var cb = PI ...
- Yii框架(一)
这里接触了 MVC 设计模式中的控制器和视图部分. 创建了一个操作作为控制器的一部分去处理特定请求. 然后又创建了一个视图去构造响应内容. 在这个小例子中,没有模型调用,唯一涉及到数据的地方是 mes ...
- Android -- ContentObserver 内容观察者
1. 实现原理图 2. 示例代码 (暂时有个问题,短信观察者 收到一条短信时 onchange方法会执行两次, 解决方法为:每次监听到变化的时候就去取最新短信的id,跟上次取的比较,如果一样的就不做处 ...
- flex-wrap:wrap弹性盒布局,自动换行
给父盒子ul{display:flex; flex-wrap:wrap; }
- 【转】linux 新建用户、用户组 以及为新用户分配权限
Linux 系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系统.用户的账号一方面可以帮助系统管理员对使用系统的用户进行 ...