本文介绍利用JQ实现输入内容过滤搜索数据功能(焦点过滤、空全部显示和模糊过滤等),效果如下:
html、css和逻辑代码都有详细的注释,可自行参考。
jq是一款轻量级的命令行json处理工具,可以帮助用户轻松处理json格式的数据。它可以从标准输入读取json数据,也可以从文件中读取。同时,它支持各种查询和过滤操作,例如选择、过滤、转换、排序和格式化等。
jq是一种针对JSON格式数据处理的命令行工具,具有以下特点:
yum install -y epel-release
[root@jeven ~]# yum search jq
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* epel: mirrors.tuna.tsinghua.edu.cn
====================================================== N/S matched: jq ======================================================
drupal7-jquery_update.noarch : Upgrades the version of jQuery in Drupal core to a newer version of jQuery
jq-devel.x86_64 : Development files for jq
js-jquery-mousewheel.noarch : A jQuery plugin that adds cross-browser mouse wheel support
js-jquery-ui.noarch : jQuery user interface
js-jquery-ui-touch-punch.noarch : Touch Event Support for jQuery UI
python-XStatic-JQuery-Migrate.noarch : JQuery-Migrate (XStatic packaging standard)
python-XStatic-JQuery-TableSorter.noarch : JQuery-TableSorter (XStatic packaging standard)
python-XStatic-JQuery-quicksearch.noarch : JQuery-quicksearch (XStatic packaging standard)
python-XStatic-jQuery.noarch : jQuery 1.10.2 (XStatic packaging standard)
python-XStatic-jquery-ui.noarch : jquery-ui (XStatic packaging standard)
python-tw2-jqplugins-flot.noarch : jQuery flot (plotting) for ToscaWidgets2
python-tw2-jqplugins-gritter.noarch : jQuery gritter (growl-like pop-ups) for ToscaWidgets2
python-tw2-jqplugins-jqplot.noarch : Toscawidgets2 wrapper for the jqPlot jQuery plugin
python-tw2-jqplugins-ui.noarch : jQuery UI for ToscaWidgets2
python-tw2-jquery.noarch : jQuery for ToscaWidgets2
jq.x86_64 : Command-line JSON processor
js-jquery.noarch : JavaScript DOM manipulation, event handling, and AJAX library
js-jquery1.noarch : JavaScript DOM manipulation, event handling, and AJAX library
nodejs-extend.noarch : Port of jQuery.extend for node.js and the browser
python-pyquery.noarch : A jQuery-like library for python
python2-XStatic-DataTables.noarch : DataTables jquery javascript framework (XStatic packaging standard)
xstatic-datatables-common.noarch : DataTables jquery javascript framework (XStatic packaging standard)
Name and summary matches only, use "search all" for everything.
yum -y install jq.x86_64
[root@docker yum.repos.d]# jq -V
jq-1.6
wget https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 -O /usr/local/bin/jq
chmod +x /usr/local/bin/jq
使用jq --help查询帮助信息
[root@jeven ~]# jq --help
jq - commandline JSON processor [version 1.6]
Usage: jq [options] <jq filter> [file...]
jq [options] --args <jq filter> [strings...]
jq [options] --jsonargs <jq filter> [JSON_TEXTS...]
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 copies jq's input to its output
unmodified (except for formatting, but note that IEEE754 is used
for number representation internally, with all that that implies).
For more advanced filters see the jq(1) manpage ("man jq")
and/or https://stedolan.github.io/jq
Example:
$ echo '{"foo": 0}' | jq .
{
"foo": 0
}
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>;
--rawfile a f set variable $a to a string consisting of the contents of <f>;
--args remaining arguments are string arguments, not files;
--jsonargs remaining arguments are JSON arguments, not files;
-- terminates argument processing;
Named arguments are also available as $ARGS.named[], while
positional arguments are available as $ARGS.positional[].
See the manpage for more options.
jq命令的选项解释
-c 紧凑而不是漂亮的输出;
-n 使用`null`作为单个输入值;
-e 根据输出设置退出状态代码;
-s 将所有输入读取(吸取)到数组中;应用过滤器;
-r 输出原始字符串,而不是JSON文本;
-R 读取原始字符串,而不是JSON文本;
-C 为JSON着色;
-M 单色(不要为JSON着色);
-S 在输出上排序对象的键;
--tab 使用制表符进行缩进;
--arg a v 将变量$a设置为value<v>;
--argjson a v 将变量$a设置为JSON value<v>;
--slurpfile a f 将变量$a设置为从<f>读取的JSON文本数组;
--rawfile a f 将变量$a设置为包含<f>内容的字符串;
--args 其余参数是字符串参数,而不是文件;
--jsonargs 其余的参数是JSON参数,而不是文件;
-- 终止参数处理;
[root@jeven ~]# cat name.json
{
"name": "John",
"age": 30,
"city": "New York",
"hobbies": ["reading", "running", "traveling"],
"education": {
"degree": "Master's",
"major": "Computer Science",
"school": "University of California"
}
}
显示json文件的所有的key
[root@jeven ~]# jq keys name.json
[
"age",
"city",
"education",
"hobbies",
"name"
]
[root@jeven ~]# jq .hobbies name.json
[
"reading",
"running",
"traveling"
]
[root@jeven ~]# jq .[] name.json
"John"
"30"
"New York"
[
"reading",
"running",
"traveling"
]
{
"degree": "Master's",
"major": "Computer Science",
"school": "University of California"
}
查询json文件内容
[root@jeven ~]# cat name.json |jq
{
"name": "John",
"age": 30,
"city": "New York",
"hobbies": [
"reading",
"running",
"traveling"
],
"education": {
"degree": "Master's",
"major": "Computer Science",
"school": "University of California"
}
}
计算name.json文件中值的长度
[root@jeven ~]# jq '.[] | length' name.json
4
2
8
3
3
[root@jeven ~]# echo '{ "jven": { "aa": { "bb": 123 } } }' | jq '.'
{
"jven": {
"aa": {
"bb": 123
}
}
}
在json文件中所有值中进行过滤内容。
们都知道现在JSON是最常用的配置和数据交换格式之一,尤其是大量的系统API接口现在基本上都是以JSON格式显示结果。JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。JSON独立于语言的文本格式,具有典型的使C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等),JSON易于人阅读和编写。同时也易于机器解析和生成。
各种语言都有大量的JSON处理库,比如fastjson,Json-lib,jsoniter,jackson,gson等,我们可以很方便就可以写一个脚本获取接口的Json信息,并通过这些类库进行处理。
虽然如此,有些同学可能还是嫌写脚本太麻烦,有没有一种很简单就能上手就用,用完就扔的JSON工具呢?答案是肯定的。这就是本文虫虫要给大家介绍的一个命令行工具jq,注意jq不是曾经流行的JS库Jquery的缩写。
jq是一个出色的命令行JSON处理器,提供了用于查询,操作和使用JSON文件的大量功能。而且作为一个命令行工具,可配合UNIX管道使用,单行脚本处理JSON。
jq是开源跨平台的软件,支持Linx,Mac OS和Windows,可通过应用包管理器、源码形式安装。
Debian和Ubuntu系:sudo apt-get install jq
redhat系:sudo yum install jq 或 sudo dnf install jq
openSUSE:sudo zypper install jq
Arch:sudo pacman -Sy jq
Mac OS:使用Homebrew安装,brew install jq
Windows:使用Chocolatey NuGet或者直接下载官方二进制包
chocolatey install jq
git clone https://github.com/stedolan/jq.git
cd jq
autoreconf -i
./configure --disable-maintainer-mode
make
sudo make install
为了方便以一个简单例子开始。首先我们调用GitHub的API,获取一个仓库的commit历史,并将其保存为example.json文件。
curl -o example.json 'https://api.github.com/repos/bollwarm/SecToolSet/commits'
要查看json内容最简单的是使用.表达式,会打印json的原始内容。
jq '.' example.json
json文件中的commit信息都是一个数组,其中一个commit可以使用.[x]操作,这和各个语言的数组操作也一样。比如:
第一个commit为.[0],以零开头。
jq '.[0]' example.json
| 操作符号是jq中的过滤器,过滤格式通过{...}来构建对象和属性,可以嵌套访问属性,例如.commit.message
下面语句获取第一个commit消息的commit.message和commit.committer.name并显示message和name:
jq '.[0] | {message: .commit.message, name: .commit.committer.name}' example.json
[]中如果为空表示获取所有的数组元素,获取所有commit消息和提交者:
jq '.[] | {message: .commit.message, name: .commit.committer.name}' example.json
jq中的数据表示为JSON流:每个jq表达式对JSON流中的各个值操作,其输出流可也包含任意数量的值。
通过仅用空格分隔JSON值即可对流进行序列化。这是cat显示友好的格式。如果想要将结果要输出作为数组形式的json格式,可以通过将过滤器用[]括住:
jq '[.[] | {message: .commit.message, name: .commit.committer.name}]' example.json
如果要获取一个commit的多个父提交的URL,由于该字段为多个元素以数组形式显示,如果直接使用.parents.html_url过滤则会下面的报错:
jq: error (at example.json:2268): Cannot index array with string "html_url"
对该类字段过滤时候,需要使用[.parents[].html_url]的数组解析格式:
jq '[.[] | {message: .commit.message, name: .commit.committer.name, parents: [.parents[].html_url]}]' example.json
我们在入门部分介绍了jq的基本使用和显示。jq作为了一个标准的shell命令行工具,也是遵循UNIX POSIX原则的,比如管道。可以通过管道对接jq的数据输入和输出,这样可以jq实现和其他工具进行交互非常。下面的管道中,cat将文件通过管道传输到jq,然后通过管道传输到less管道。这对于查看大型JSON文件非常有用。
cat example.json | jq '.' | less
当然用管道对接less着色就失效了,这是一个副作用。
为了找到键和值,jq可以根据键进行过滤并返回值。入门部分我们已经讲了过滤键。我们再举一个简单例子,假设一个如下的JSON文档保存为dog.json。
jq可以通过在表达式中使用.键名来搜索值。
jq '.name' dog.json
CC
多个键搜索,中间用逗号分开:
jq '.breed,.name' dog.json
"金毛"
"CC"
可以将键名自定义:
jq '{"主人":.owner,"爱好":.likes}' dog.json
入门部分我们说过,要搜索数组中的项目,请使用括号语法,索引从0开始。
jq '.likes[1]' dog.json
"球球"
数组的多个元素也可能返回。
echo '["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",",","!"]'|jq '[.[12],.[4],.[17],.[17],.[24],.[26],.[2],.[7],.[17],.[8],.[18],.[19],.[12],.[0],.[18],.[27]]
结果:" MERRY,CHRISTMAS!" 祝大家圣诞快乐!
jq不仅可以用于从JSON对象获取值显示,而且还可以用于JSON转换为新的数据结构。比如:对dog.json 可以创建一个包含狗名和爱好的键,组成一个新数组。
jq '[.name,.likes[]]' dog.json
[
"CC",
"骨头",
"球球",
"狗粮"
]
需要把JSON数据从一种结构转移到另一种数据结构的数据转换时,非常有用。
jq还可以对JSON对象中的数据进行操作。
echo '{"a": 1 , "b": 2}'|jq '.a+100'
101
其实上,+对字符串也是适用的,比如前面的圣诞快乐单行也可以为:
echo '["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",",","!"]'|jq '[.[12]+.[4]+.[17]+.[17]+.[24]+.[26]+.[2]+.[7]+.[17]+.[8]+.[18]+.[19]+.[12]+.[0]+.[18]+.[27]]'
结果:
[
"MERRY,CHRISTMAS!"
]
jq也支持从JSON对象中删除键。删除后输出就不包含删除key的JSON对象。删除键使用del()函数,还是以dog.json为例:
jq 'del(.owner)' dog.json
结果中就不包括owner键了:
{
"name": "CC",
"breed": "金毛",
"age": "4",
"likes": [
"骨头",
"球球",
"狗粮"
]
}
jq 'del(.)' dog.json
null
所有键都被删了,所以上面结果为null。
jq可以映射值并在每个值上执行操作。在下面的示例中,数组中的每个键进行映射并做数值计算加2。
echo '[1,2,3,4,5,6]' |jq 'map(.+2)'
[
3,
4,
5,
6,
7,
8
]
本文介绍了一个命令行下的json解析神器,更多文档可以参考官方网站,可以托管仓库的wiki页。
*请认真填写需求信息,我们会在24小时内与您取得联系。