[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]); 执行函数后不输出结果,返回最 ...
随机推荐
- uboot源码中"include/configs/$(boardname).h"与"configs/$(boardname)_defconfig"之间有何异同
答:最大的不同就是"configs/boardname_defconfig"中的选项都可以在make menuconfig中进行配置,而"include/configs/ ...
- 从零开始玩转JMX(二)——Condition
Notification 一个MBean提供的管理接口允许代理对其管理资源进行控制和配置.然而,对管理复杂的分布式系统来说,这些接口知识提供了一部分功能.通常,管理应用程序需要对状态变化或者当特别情况 ...
- Python基础笔记系列十一:标准输入输出、文件读写和指针等操作
本系列教程供个人学习笔记使用,如果您要浏览可能需要其它编程语言基础(如C语言),why?因为我写得烂啊,只有我自己看得懂!! 标准输入输出一.输入 在sublime中这个时候需要安装SublimeRE ...
- 用svg实现不规则形状
像这种弧形,用纯html和css很难写,但是用svg就简单多了. 可以用作图工具画出一个弧形,然后导成svg格式.在页面中,下面的白块就是div+svg构成 mixin svgCard(...cont ...
- mysql数据库优化课程---17、mysql索引优化
mysql数据库优化课程---17.mysql索引优化 一.总结 一句话总结:一些字段可能会使索引失效,比如like,or等 1.check表监测的使用场景是什么? 视图 视图建立在两个表上, 删除了 ...
- c语言memset源码
c语言memset源码 一.用法 void *memset(void *s, int ch, size_t n);作用:将s所指向的某一块内存中的每个字节的内容全部设置为ch指定的ASCII值, 块的 ...
- Linux shell常用命令
1. sz 和 rz sz命令发送文件到本地: # sz filename rz命令本地上传文件到服务器: # rz 执行该命令后,在弹出框中选择要上传的文件即可.
- Enumerable的判断是否包含某个元素
// 通过使用默认的相等比较器(即纯粹的对象比较)确定序列是否包含指定的元素. public static bool Contains<TSource>(this IEnumerable& ...
- FontAwesome::Sass(5.x版)使用帮助。
FontAwesome::Sass(5.x版) https://fontawesome.com/icons?d=gallery&m=free 只能使用免费的. 在app/assets/styl ...
- jdk动态代理使用及原理
jdk动态代理的使用 1.创建实现InvocationHandler接口的类,实现invoke(Object proxy, Method method, Object[] args)接口,其中invo ...