首页 > 系统相关 >Linux系统之jq工具的基本使用

Linux系统之jq工具的基本使用

时间:2023-09-02 12:00:51浏览次数:37  
标签:jQuery -- json jq noarch JSON Linux 工具

(Linux系统之jq工具的基本使用)

一、jq工具介绍

1. jq工具简介

jq是一款轻量级的命令行json处理工具,可以帮助用户轻松处理json格式的数据。它可以从标准输入读取json数据,也可以从文件中读取。同时,它支持各种查询和过滤操作,例如选择、过滤、转换、排序和格式化等。

2. jq工具的特点

jq是一种针对JSON格式数据处理的命令行工具,具有以下特点:

  • 快速和高效:jq使用C语言编写,处理JSON数据非常快速和高效。

  • 灵活和强大:jq具有丰富的功能和灵活的语法,能够处理复杂的JSON数据结构和进行高级的JSON数据操作。

  • 命令行工具:jq是一个命令行工具,可从终端中直接调用,方便快捷。

  • 跨平台:jq可在多种操作系统(包括Unix/Linux、Windows和macOS)上使用。

  • 开源:jq是一个开源工具,用户可自由修改和分发。

  • 支持管道:jq支持从一个命令输出管道到另一个命令作为输入,使得数据处理更加灵活和高效。

二、jq工具的安装

1. yum安装jq

  • 需要提前安装epel源
yum install -y epel-release
  • 搜索jq命令的软件包
[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.

  • 安装jq工具
yum -y install jq.x86_64
  • 查看jq版本
[root@docker yum.repos.d]# jq -V
jq-1.6

2. 二进制安装jq

  • 下载jq工具的软件包
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命令的使用帮助

1. 查询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.

2. jq命令的选项解释

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参数,而不是文件;
--               终止参数处理;

四、jq命令的基本使用

1. 显示json文件的所有的key

  • 查看当前测试name.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"
]

2. 显示key对应的值

  • 显示某个key对应的值
[root@jeven ~]# jq .hobbies  name.json 
[
  "reading",
  "running",
  "traveling"
]

  • 显示所有key对应的值
[root@jeven ~]# jq .[] name.json 
"John"
"30"
"New York"
[
  "reading",
  "running",
  "traveling"
]
{
  "degree": "Master's",
  "major": "Computer Science",
  "school": "University of California"
}

3. 查询json文件

查询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"
  }
}

4. 计算值的长度

计算name.json文件中值的长度

[root@jeven ~]# jq '.[] | length' name.json 
4
2
8
3
3

5. 输出美观的格式

[root@jeven ~]# echo '{ "jven": { "aa": { "bb": 123 } } }' | jq '.'
{
  "jven": {
    "aa": {
      "bb": 123
    }
  }
}

6. 过滤json文件中的键值

在json文件中所有值中进行过来吧内容。

[root@jeven ~]# jq .[] name.json  | grep school
  "school": "University of California"

标签:jQuery,--,json,jq,noarch,JSON,Linux,工具
From: https://blog.51cto.com/u_14664141/7331881

相关文章

  • SAP一个不以录屏方式批量导入Storage Bin的LSMW工具
    SAP一个不以录屏方式批量导入StorageBin的LSMW工具笔者在之前的项目上,导入SAPWM里的storagebin主数据多是使用录屏模式的LSMW工具。笔者目前所在的项目上,发现了还可以使用DirectInput的方式来导入StorageBin主数据,这让笔者很是兴奋。所以在自己的IDES系统上做了一个这样的LSMW......
  • linux shell脚本:创建数据库、数据库同名用户及赋权
    调用脚本如果mysql中已经有了一个mydb0831,会先drop掉,再创建一个空的mydb0831库,同时创建一个名为mydb0831的用户,对这个mydb0831库拥有所有权限。如果机器里未安装mysql,脚本执行会报错。可以从其他相同操作系统安装了mysql的机器里/usr/bin/下复制mysql过来,赋予可执行权限,即可。./cr......
  • Linux文件系统及存储
                   ......
  • 在linux上部署lavavel项目提示log目录没权限的问题
    Thestreamorfile"/data/www/laravel/storage/logs/laravel.log"couldnotbeopened:failedtoopenstream:PermissiondenieWeb服务器用户和组依赖于你的Web服务器和操作系统。找出你的Web服务器用户和组使用以下命令的内容。对于nginx使用: ......
  • Linux less 命令
    less与more类似,less可以随意浏览文件,支持翻页和搜索,支持向上翻页和向下翻页。语法less[参数]文件参数说明:-b<缓冲区大小>设置缓冲区的大小-e当文件显示结束后,自动离开-f强迫打开特殊文件,例如外围设备代号、目录和二进制文件-g只标志最后搜索的关键词-i忽略搜......
  • Rocky_linux9网卡启动失败问题记录
    一、故障场景之前虚拟机一直是可以上网的,昨天正常关机后第二天开机网卡始终启动不了。开始排查问题查看网卡信息,发现获取不到IP地址查看网卡状态时发现处于未连接状态nmclicshow二、尝试启动网卡1)直接启用网卡nmclicupens37启动失败出现报错信息,连接激活失败,找不到适合此连接......
  • Apache IoTDB开发之Watermark工具
    Watermark工具该工具有两个功能:IoTDB查询结果的Watermark嵌入可疑数据的Watermark检测1.Watermark嵌入1.1配置默认情况下,IoTDB中的Watermark处于禁用状态。要启用Watermark嵌入,首先要修改配置文件中的以下字段:iotdb-engine.properties名字例解释watermark_module_opened假true启......
  • 实用的文件编辑工具:UltraISO软碟通
    UltraISO软碟通是一个类似于WinISO的ISO文件编辑工具,UltraISO软碟通操作简单,界面简洁,用户可以用来制作启动光盘映像。不仅如此,UltraISO软碟通还可以用来处理ISO文件的启动信息,随心所欲烧录光碟。UltraISO软碟通软件简介UltraISO软碟通是一款功能强大而又方便实用的软碟文件制作/编......
  • disable/enable an elment using jQuery & JS
    1.JS:  2.jQuery: 参考网址:https://stackoverflow.com/questions/13831601/disabling-and-enabling-a-html-input-buttonhttps://stackoverflow.com/questions/1414365/disable-enable-an-input-with-jquery ......
  • linux yum源配置
    一、配置本地光盘镜像源1、挂载光盘镜像mount/dev/sr0/mnt/cdrom2、修改yum源配置文件[root@yum~]#vim/etc/yum.repos.d/CentOS7.9.repo[base-local]本源的名称name名称baseurl源路径enabled是否启用1-开启0-不开启gpgcheck是否安全检测1-开启0-不开启gp......