本文介紹利用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 將所有輸入讀?。ㄎ。┑綌到M中;應用過濾器;
-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格式,可以通過將過濾器用[]括?。?/p>
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小時內與您取得聯系。