[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]); 执行函数后不输出结果,返回最 ...
随机推荐
- IO多路复用客户端-服务器模型
IO多路复用服务器 -- 实现字符回射 服务器端 /************************************************************************* ...
- ubuntu文字界面与图形界面切换
redhat: (据说是) 图形界面->文字界面:crtl+alt+F1~6 文字界面->图形界面:crtl+alt+F7 -------------------------------- ...
- 采用OpenReplicator解析MySQL binlog
Open Replicator是一个用Java编写的MySQL binlog分析程序.Open Replicator 首先连接到MySQL(就像一个普通的MySQL Slave一样),然后接收和分析b ...
- eclipse 工程没有build path
项目的.project文件添加: <buildSpec><buildCommand><name>org.eclipse.jdt.core.javabuilder&l ...
- Hibernate的懒加载session丢失解决方法
在web.xml加入spring提供的过滤器,延长session的生命周期 <!--Hibernate的懒加载session丢失解决方法 --> <filter> <fi ...
- Css常用属性单位
长度单位:px-像素 颜色单位: ①十六进制:#FFFFFF: ②颜色名称:red: ③RGB颜色代码:RGB(0-255,0-255,0-255): ...
- 编译binutil包报错 error: array type has incomplete element type extern const struct relax_type md_relax_table[];
安装lfs时编译binutils出错: ../../sources/binutils-2.15.91.0.2/gas/config/tc-i386.h:457:32: error: array typ ...
- npm install 报错 ECONNREFUSED
在window环境下,使用npm install 命令安装任何框架,都会报如下的错误 error code ECONNREFUSED error errno ECONNREFUSED error Fe ...
- hiho 1323 : 回文字符串 dp
#1323 : 回文字符串 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个字符串 S ,最少需要几次增删改操作可以把 S 变成一个回文字符串? 一次操作可以在任 ...
- jquery插件之jquery-ui
1.jQuery UI是在jQuery的基础上,利用jQuery的扩展性,设计的插件.提供了一些常用的界面元素,如:模态框.日期控件等. 2.下载地址:http://jqueryui.com/down ...