如何使用jq从json输出中提取某些字段?

时间:2019-11-12 17:29:17

标签: json jq

使用jq,如何从下面的json输出中提取主机,名称和date_happened字段?我在stackoverflow上阅读了与此相关的另一个线程,但似乎仍然无法获得它。

-谢谢

{
    "events": [
        {
            "alert_type": "success",
            "children": [
                {
                    "alert_type": "error",
                    "date_happened": 1573502725,
                    "id": "5188183926379101887"
                },
                {
                    "alert_type": "success",
                    "date_happened": 1573503145,
                    "id": "5188190972457497744"
                }
            ],
            "comments": [],
            "date_happened": 1573502725,
            "device_name": null,
            "host": "i-0e4b192579a9b423b",
            "id": 5188183933173874377,
            "is_aggregate": true,
            "priority": "normal",
            "resource": "/api/v1/events/5188183933173874377",
            "source": "Monitor Alert",
            "tags": [
                "autoscaling_group:app2_backend-asg-prod",
                "availability-zone:us-east-1b",
                "datadog-agent:true",
                "environment:prod",
                "host:i-0e6b192579a9b423b",
                "iam_profile:app2_backend_instance_profile",
                "image:ami-2769055d",
                "instance-type:m4.large",
                "monitor",
                "name:app2_backend-prod",
                "region:us-east-1",
                "role:app2_backend",

1 个答案:

答案 0 :(得分:0)

jq过滤器:

.events[]
| [.host, 
   (.tags[] | select( test("^name:") ) | sub("name:";"")),
   .date_happened]

产生

 ["i-0e4b192579a9b423b","app2_backend-prod",1573502725]