linux 格式化输出json数据

今天我在Linux调用一个服务的api的时候,服务给我返回的是一份Json类型的数据,但是显示很乱,没有做json格式化。如下:

[root@k8snode01 prometheus]# curl 'http://localhost:19090/api/v1/targets?state=active' 
{"status":"success","data":{"activeTargets":[{"discoveredLabels":{"__address__":"192.168.12.90:9100","__metrics_path__":"/metrics","__scheme__":"http","cluster_manager":"yilei","job":"node_exporter"},"labels":{"cluster_manager":"yilei","instance":"192.168.12.90:9100","job":"node_exporter"},"scrapePool":"node_exporter","scrapeUrl":"http://192.168.12.90:9100/metrics","globalUrl":"http://192.168.12.90:9100/metrics","lastError":"","lastScrape":"2021-03-22T13:51:28.439400621+08:00","lastScrapeDuration":0.016126826,"health":"up"},{"discoveredLabels":{"__address__":"192.168.12.92:9100","__metrics_path__":"/metrics","__scheme__":"http","cluster_manager":"yilei","job":"node_exporter"},"labels":{"cluster_manager":"yilei","instance":"192.168.12.92:9100","job":"node_exporter"},"scrapePool":"node_exporter","scrapeUrl":"http://192.168.12.92:9100/metrics","globalUrl":"http://192.168.12.92:9100/metrics","lastError":"Get \"http://192.168.12.92:9100/metrics\": context deadline exceeded","lastScrape":"2021-03-22T13:51:09.930680241+08:00","lastScrapeDuration":10.00045077,"health":"down"},{"discoveredLabels":{"__address__":"localhost:19090","__metrics_path__":"/metrics","__scheme__":"http","job":"prometheus"},"labels":{"instance":"localhost:19090","job":"prometheus"},"scrapePool":"prometheus","scrapeUrl":"http://localhost:19090/metrics","globalUrl":"http://k8snode01:19090/metrics","lastError":"","lastScrape":"2021-03

于是百度了一下,得到的结果是,按照 jq 插件!

安装方式如下:

wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -ivh epel-release-latest-7.noarch.rpm
yum repolist

yum install jq

安装完后,测试一下命令:

[root@k8snode01 prometheus]# jq
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
	}

For a listing of options, use jq --help.
[root@k8snode01 prometheus]# 

再访问一下之前的接口: 原先接口的后面,拼接一个  |jq .

curl 'http://localhost:19090/api/v1/targets?state=active' |jq .

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一彡十

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值