bash格式化输出python

2023年 7月 16日 60.0k 0

我们在日常工作中,有时候需要访问一些API接口,这些接口都是以json格式返回的,但是在shell中,默认对json显示不是很友好,如下所示:

[root@c7-node1 ~]# curl -s http://192.168.28.71:9200/_tasks
{"nodes":{"iJIboMLLRwWwai1WWSXFMg":{"name":"iJIboML","transport_address":"192.168.28.71:9300","host":"192.168.28.71","ip":"192.168.28.71:9300","roles":["master","data","ingest"],"tasks":{"iJIboMLLRwWwai1WWSXFMg:42373":{"node":"iJIboMLLRwWwai1WWSXFMg","id":42373,"type":"transport","action":"cluster:monitor/tasks/lists","start_time_in_millis":1520940440161,"running_time_in_nanos":956890,"cancellable":false},"iJIboMLLRwWwai1WWSXFMg:42374":{"node":"iJIboMLLRwWwai1WWSXFMg","id":42374,"type":"direct","action":"cluster:monitor/tasks/lists[n]","start_time_in_millis":1520940440162,"running_time_in_nanos":297574,"cancellable":false,"parent_task_id":"iJIboMLLRwWwai1WWSXFMg:42373"}}}}}

如果我们想要在bash中输出json格式,可以将URL的返回结果,传给python来进行解析,如下所示:

[root@c7-node1 ~]# curl -s http://192.168.28.71:9200/_tasks | python -mjson.tool
{
    "nodes": {
        "iJIboMLLRwWwai1WWSXFMg": {
            "host": "192.168.28.71",
            "ip": "192.168.28.71:9300",
            "name": "iJIboML",
            "roles": [
                "master",
                "data",
                "ingest"
            ],
            "tasks": {
                "iJIboMLLRwWwai1WWSXFMg:42379": {
                    "action": "cluster:monitor/tasks/lists",
                    "cancellable": false,
                    "id": 42379,
                    "node": "iJIboMLLRwWwai1WWSXFMg",
                    "running_time_in_nanos": 328807,
                    "start_time_in_millis": 1520940460219,
                    "type": "transport"
                },
                "iJIboMLLRwWwai1WWSXFMg:42380": {
                    "action": "cluster:monitor/tasks/lists[n]",
                    "cancellable": false,
                    "id": 42380,
                    "node": "iJIboMLLRwWwai1WWSXFMg",
                    "parent_task_id": "iJIboMLLRwWwai1WWSXFMg:42379",
                    "running_time_in_nanos": 40144,
                    "start_time_in_millis": 1520940460219,
                    "type": "direct"
                }
            },
            "transport_address": "192.168.28.71:9300"
        }
    }
}

相关文章

服务器端口转发,带你了解服务器端口转发
服务器开放端口,服务器开放端口的步骤
产品推荐:7月受欢迎AI容器镜像来了,有Qwen系列大模型镜像
如何使用 WinGet 下载 Microsoft Store 应用
百度搜索:蓝易云 – 熟悉ubuntu apt-get命令详解
百度搜索:蓝易云 – 域名解析成功但ping不通解决方案

发布评论