linux 格式化输出json数据
今天我在Linux调用一个服务的api的时候,服务给我返回的是一份Json类型的数据,但是显示很乱,没有做json格式化。如下:[root@k8snode01 prometheus]# curl 'http://localhost:19090/api/v1/targets?state=active'{"status":"success","data":{"activeTargets":[{"dis
·
今天我在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条内容
所有评论(0)