[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]); 执行函数后不输出结果,返回最 ...
随机推荐
- php实现dota天梯、wow竞技场、lol排位赛匹配加分算法ELO
public function marchOpponents() { $rstep = Yii::$app->params['ratingStep'];//(随机范围) $rsN=100; $d ...
- ubuntu 16.04 gtx1060 显卡安装【转】
本文转载自:https://blog.csdn.net/u010925447/article/details/79754044 版权声明:本文为博主原创文章,未经博主允许不得转载. https://b ...
- Redis-与python交互
安装包 到中文官网查找客户端代码 联网安装 sudo pip install redis 使用源码安装 unzip redis-py-master.zip cd redis-py-master sud ...
- POJ-2418 Hardwood Species(二叉搜索树)
思路就是先将每个单词存进二叉树中,没出现一次,修改该单词所在结点的cnt++: 最后通过递归中序遍历输出结果. 思路很清晰,主要注意一下指针的使用,想一想为什么要这么用? 简单的解释就是,insert ...
- integration asp.net web api with autofac and owin
There is an example project showing Web API in conjunction with OWIN self hosting https://github.com ...
- hadoop项目实战--ETL--(一)项目分析
项目描述 一 项目简介 在远程服务器上的数据库中有两张表,user 和order,现需要对表中的数据做分析,将分析后的结果再存到mysql中.两张表的结构如下图所示 现需要分析每一天user和,ode ...
- Codeforces Round #365 (Div. 2) B - Mishka and trip
http://codeforces.com/contest/703/problem/B 题意: 每个点都有一个值,每条边的权值为这两个点相乘.1~n成环.现在有k个城市,城市与其他所有点都相连,计算出 ...
- SVM(三)线性支持向量机
本文是在微信公众号发表的原创~ 额,图片粘不过来~就把链接给你们吧 http://mp.weixin.qq.com/s?__biz=MjM5MzM5NDAzMg==&mid=400740076 ...
- Outlook.com 系列邮箱 POP3 及 IMAP 设置方法
支持 Exchange ActiveSync 的应用 有了 EAS,你可以立即获取电子邮件,以及在一个位置查看所有文件夹.日历和联系人. 如果你的电子邮件应用支持Exchange ActiveSync ...
- codeforces103E Buying Sets
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...