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

参考:

linux 命令之jq

jq : Linux下json的命令行工具

命令行 JSON 处理工具 jq 的使用介绍

jq : Linux下json的命令行工具-Bean_lee-ChinaUnix博客

[Linux] jq:命令行JSON处理工具的更多相关文章

  1. Linux中命令行终端切换工具screen

    screen命令 本文转自:http://man.linuxde.net/screen Screen是一款由GNU计划开发的用于命令行终端切换的自由软件.用户可以通过该软件同时连接多个本地或远程的命令 ...

  2. VMware10中的CentOS6.5命令行安装VMwaretools工具启用windows与虚拟机中Linux系统的共享目录

    VMware10中的CentOS6.5命令行安装VMwaretools工具启用windows与虚拟机中Linux系统的共享目录 一.描述 系统描述:win7旗舰版64位系统+VMware Workst ...

  3. 使用Linux的命令行工具做简单的文本分析

    Basic Text Analysis with Command Line Tools in Linux | William J Turkel 这篇文章非常清楚的介绍了如何使用Linux的命令行工具进 ...

  4. Linux系统——28个命令行下的工具

    Unix/Linux下的28个命令行下的工具 下面是Kristóf Kovács收集的28个Unix/Linux下的28个命令行下的工具(原文链接),有一些是大家熟悉的,有一些是非常有用的,有一些是不 ...

  5. 28个Unix/Linux的命令行神器_转

    28个Unix/Linux的命令行神器 下面是Kristóf Kovács收集的28个Unix/Linux下的28个命令行下的工具,有一些是大家熟悉的,有一些是非常有用的,有一些是不为人知的.这些工具 ...

  6. APICloud提供适用于命令行的开发工具,开发更具极客精神!

    APICloud近期大动作不断,上周刚刚支持Atom编辑器,并推出核心开发工具库.本周又为开发者提供了一款超轻便的新开发工具--CLI工具! 操作系统: Mac/Windows/Linux nodej ...

  7. 28个Unix/Linux的命令行神器

    下面是Kristóf Kovács收集的28个Unix/Linux下的28个命令行下的工具(原文链接),有一些是大家熟悉的,有一些是非常有用的,有一些是不为人知的.这些工具都非常不错,希望每个人都知道 ...

  8. 28 个 Unix/Linux 的命令行神器

    28 个 Unix/Linux 的命令行神器   下面是Kristóf Kovács收集的28个Unix/Linux下的28个命令行下的工具(原文链接),有一些是大家熟悉的,有一些是非常有用的,有一些 ...

  9. php 运行linux命令 与 linux下命令行执行php

    1.php运行linux命令 exec函数:string exec(string command, string [array], int [return_var]);  执行函数后不输出结果,返回最 ...

随机推荐

  1. 再也不学AJAX了!(二)使用AJAX

    在上一篇文章中我们知道,AJAX是一系列技术的统称.在本篇中我们将更进一步,详细解释如何使用Ajax技术在项目中获取数据.而为了解释清楚,我们首先要搞清楚我们是从哪里获取数据的,其次我们关注的才是获取 ...

  2. SpringBoot 打包为Docker进行

    可以有两种方式: 1.dockerfile 2.maven docker 第一种方式:通过dockerfile打包Docker镜像 1.将dockerfile和 springboot打包的jar文件放 ...

  3. java中把指数形式的数字转为正常形式显示

    /** * 当浮点型数据位数超过10位之后,数据变成科学计数法显示.用此方法可以使其正常显示. * @param value * @return Sting */ public static Stri ...

  4. NAT模式下远程连接centos6虚拟机与虚拟机网络配置

    最近装了centos,但是没有网络,也无法远程连接.关键是虚拟机中没有ip地址. 网上方法很多,但是每个人情况不一样,所以不尽适用. 1.解决这个问题,首先保证你的vmware的dhcp服务和net服 ...

  5. HIVE分组排序问题

    答案: hive> select *,row_number() over (partition by product_no order by start_time desc) from tabl ...

  6. 【Semantic segmentation】Fully Convolutional Networks for Semantic Segmentation 论文解析

    目录 0. 论文链接 1. 概述 2. Adapting classifiers for dense prediction 3. upsampling 3.1 Shift-and-stitch 3.2 ...

  7. Python学习札记(十一) Function2 函数定义

    参考:定义函数 Note: 先看一段代码实例(Barefoot topo.py): def read_topo(): nb_hosts = 0 nb_switches = 0 links = [] w ...

  8. 04_MySQL常见函数_单行函数

    #单行函数细分1.字符函数2.数学函数3.日期函数4.其他函数5.流程控制函数 #单行函数 - 字符函数#一.字符函数#1. length 获取参数的字节长度SELECT LENGTH('john') ...

  9. Asp.Net MVC 请求原理分析

    分析Asp.Net MVC的请求过程,我们从以下几方面看: 配置:IIS网站的配置可以分为两个块:全局 Web.Config 和本站 Web.Config . Asp.Net Routing属于全局性 ...

  10. try throw catch typeid

    QString str = ui.ll->text(); try { if (str == NULL) { throw 1; } else { throw 1.2; } } catch (int ...