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文件中所有值中进行过来吧内容。
[root@jeven ~]# jq .[] name.json | grep school
"school": "University of California"
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。