使用 jq 删除空结果

Remove empty results with jq

我想删除空结果,目前看起来像这样

{
  "profile": "test1",
  "services": [
    {
      "name": "app",
      "version": "1.3.2"
    }
  ]
}
{
  "profile": "test2",
  "services": []
}

我不想显示 test2,因为它是空的。 这是我当前的命令 运行

cat data.json | jq '.[] | {profile: .profile, services: [.clusters[].services[]| select(.taskdef[] | .name=="app" and .version=="1.3.2") | .taskdef[] ]}'

这是 data.json

的样本
[{
    "profile": "test1",
    "clusters": [{
            "cluster": "test1-cluster1",
            "services": [{
                "servicename": "cluster1-service",
                "taskdef": [{
                    "name": "app",
                    "version": "1.3.3"
                }]
            }]
        },
        {
            "cluster": "test1-cluster2",
            "services": [{
                "servicename": "cluster2-service",
                "taskdef": [{
                    "name": "app",
                    "version": "1.3.2"
                }]
            }]
        }
    ]
},
{
    "profile": "test2",
    "clusters": [{
        "cluster": "test2-cluster1",
        "services": [{
            "servicename": "cluster1-service",
            "taskdef": [{
                "name": "app",
                "version": "1.4.0"
            }]
        }]
    }]
}

]

我一直在试验 select( lenght > 0),但语法不正确。

您只需将以下内容附加到现有管道即可:

| select(.services!=[])

或者您可以重写查询:

.[]
| [.clusters[].services[] 
   | select(.taskdef[] | .name=="app" and .version=="1.3.2") 
   | .taskdef[] ] as $services
| select($services!=[])
| {profile, $services}