通过匹配多个对象来用jq过滤JSON对象列表 [英] Filtering JSON object list with jq by matching multiple objects

查看:101
本文介绍了通过匹配多个对象来用jq过滤JSON对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试过滤"aws elb describe-tags"的输出,并通过匹配三个标签来获得LoadBalancerName.我没有看到如何选择对象列表的特定元素并比较键和值.我要针对三个对象,如果它们都匹配,则需要返回LoadBalancerName.

I'm trying to filter the output of 'aws elb describe-tags' and get a LoadBalancerName by matching three tags. I'm not seeing how I can select a specific element of an object list and compare both Key and Value. I have three objects to look against, and if they're all a match I need to return LoadBalancerName.

以下是三个负载均衡器的示例输出,其中只有一个带有正确的标记集.

Here's an example output of three load balancers, with only one having the correct set of tags.

{
    "TagDescriptions": [
        {
            "Tags": [
                {
                    "Value": "production",
                    "Key": "environment"
                },
                {
                    "Value": "widget",
                    "Key": "service"
                },
                {
                    "Value": "widget_xyz",
                    "Key": "customer_prefix"
                },
                {
                    "Value": "widget_xyz-widget-production",
                    "Key": "Name"
                }
            ],
            "LoadBalancerName": "widget-xyz-widget-prod"
        },
        {
            "Tags": [
                {
                    "Value": "widget-xyz-stage-widget-ConsulStack-DKJSADKJS",
                    "Key": "aws:cloudformation:stack-name"
                },
                {
                    "Value": "stage",
                    "Key": "environment"
                },
                {
                    "Value": "arn:aws:cloudformation:us-east-1:123456789:stack/widget-xyz-stage-widget-ConsulStack-DKJSADKJS/d46ad520-92e7-11e5-a975-500150b34c7c",
                    "Key": "aws:cloudformation:stack-id"
                },
                {
                    "Value": "widget",
                    "Key": "service"
                },
                {
                    "Value": "widget_xyz",
                    "Key": "customer_prefix"
                },
                {
                    "Value": "ELB",
                    "Key": "aws:cloudformation:logical-id"
                },
                {
                    "Value": "widget_xyz-widget-stage",
                    "Key": "Name"
                }
            ],
            "LoadBalancerName": "widget-xyz-ELB-SDKJSDKJSADKJAS"
        },
        {
            "Tags": [
                {
                    "Value": "widget-xyz-prod-widget-ConsulStack-DLFJEIJNWDKD",
                    "Key": "aws:cloudformation:stack-name"
                },
                {
                    "Value": "prod",
                    "Key": "environment"
                },
                {
                    "Value": "arn:aws:cloudformation:us-east-1:123456789:stack/widget-xyz-prod-widget-ConsulStack-DLFJEIJNWDKD/ab2292f0-9398-11e5-b0f6-50d501114c2c",
                    "Key": "aws:cloudformation:stack-id"
                },
                {
                    "Value": "widget",
                    "Key": "service"
                },
                {
                    "Value": "widget_xyz",
                    "Key": "customer_prefix"
                },
                {
                    "Value": "ELB",
                    "Key": "aws:cloudformation:logical-id"
                },
                {
                    "Value": "widget_xyz-widget-prod",
                    "Key": "Name"
                }
            ],
            "LoadBalancerName": "widget-xyz-ELB-SKDJSKDJSAKDJAS"
        }
    ]
}

我已经成功实现了查询,但是并不安全.只要任何三个值与我的搜索模式都匹配,它就会返回LoadBalancerName.我想搜索一个特定的键,然后比较值.

I've successfully implemented my query, but unsafely. It's returning the LoadBalancerName as long as any three values match my search pattern. I would like to search for a specific Key, and then compare the Value.

这是我不安全的查询,该查询已在要点上的代码段上成功执行.它返回widget-xyz-widget-prod,这是我想要获取的参数.

Here's my unsafe query that is successful on the snippit on gist. It returns widget-xyz-widget-prod, which is the parameter I'm looking to get.

jq --raw-output '.TagDescriptions[] | select(.Tags[].Value=="widget_xyz") | select(.Tags[].Value=="widget") | select(.Tags[].Value=="production") | .LoadBalancerName'

jq --raw-output '.TagDescriptions[] | select(.Tags[].Value=="widget_xyz") | select(.Tags[].Value=="widget") | select(.Tags[].Value=="production") | .LoadBalancerName'

如果所有三个条件都为真,则应返回:

It should return if all three conditions are true:

Key == "service" && Value == "widget"
Key == "environment" && Value == "production"
Key == "customer_prefix" && Value == "widget_xyz"

您可以在上面的查询中看到,我只是比较Value.

As you can see in my query above I am only comparing Value.

更新:我已经能够构造一个查询,以过滤匹配一个对象的键和值,但是我仍在尝试匹配多个对象.

UPDATE: I've been able to construct a query that filters against matching both Key and Value from one object, but I'm still trying to work on matching more than one object.

.TagDescriptions[] | select(.Tags[].Key=="customer_prefix" and .Tags[].Value == "widget_xyz") | .LoadBalancerName

另一个更新:好的,我已经能够一起破解一个查询.我感觉好像仍然缺少一个难题,使用我还不了解的jq的一些精巧功能可以大大简化此查询.

ANOTHER UPDATE: Ok, so, I've been able to hack a query together. I feel as if I'm still missing a piece of the puzzle, and this query can be greatly simplified using some slick feature of jq that I have yet to understand.

.TagDescriptions[] | [select(.Tags[].Key == "customer_prefix" and .Tags[].Value == "widget_xyz")][] | [select(.Tags[].Key == "environment" and .Tags[].Value == "production")][] | [select(.Tags[].Key == "service" and .Tags[].Value == "widget")][] | .LoadBalancerName

推荐答案

Tags数组非常适合创建易于访问的对象.让自己变得轻松,做到这一点.然后,访问这些值将变得更加容易.然后,您可以轻松地测试一下是否满足您的条件.

The Tags array are perfect for creating an object out of for easy access. Make things easy on yourself and do so. Then access to the values would be significantly easier. Then you could easily test to see if your conditions are satisfied.

.TagDescriptions[] | select(
    .Tags | from_entries | [
        .service == "widget",
        .environment == "production",
        .customer_prefix == "widget_xyz"
    ] | all
).LoadBalancerName

这篇关于通过匹配多个对象来用jq过滤JSON对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆