jq:从另一个数组元素中按值搜索 [英] jq: search by value from another array element

查看:50
本文介绍了jq:从另一个数组元素中按值搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列不同类型的对象,这些对象彼此之间通过UUID(一个terraform.tfstate文件)进行引用.我想根据另一个对象中另一个值的出现,从一个这样的对象中选择一个值,其中两个对象由其中一个UUID关联.

I have an array of objects of various types which reference one another with UUIDs (a terraform.tfstate file). I'd like to select one value from one such object based on the appearance of a different value in another object, where the two objects are related by one of those UUIDs.

通过示例,我可以做到这一点:

By way of example, I can do this:

$ jq '.modules[].resources[]
| select(.type == "openstack_compute_instance_v2" and 
         .primary.attributes.name == "jumpbox").primary.id' terraform.tfstate
"5edfe2bf-94df-49d5-8118-3e91fb52946b"
$ jq '.modules[].resources[] 
| select(.type =="openstack_compute_floatingip_associate_v2" and 
         .primary.attributes.instance_id == "5edfe2bf-94df-49d5-8118-3e91fb52946b").primary.attributes.floating_ip' terraform.tfstate
"10.120.241.21"

根据名称向我提供跳箱" VM的外部浮动IP.

Giving me the external floating IP of the 'jumpbox' VM based on its name.

我想打所有一个jq电话.有可能吗?

I'd like to make that all one jq call. Is that possible?

推荐答案

如果您提供了更多示例数据,则答案会更容易 从您的命令向后工作(重新格式化)

This would be easier to answer if you provided more sample data but working backwards from your commands (with some reformatting)

$ jq '
      .modules[].resources[]
    | select(.type == "openstack_compute_instance_v2" and .primary.attributes.name == "jumpbox")
    | .primary.id
' terraform.tfstate
"5edfe2bf-94df-49d5-8118-3e91fb52946b"

$ jq '
      .modules[].resources[]
    | select(.type =="openstack_compute_floatingip_associate_v2" and .primary.attributes.instance_id == "5edfe2bf-94df-49d5-8118-3e91fb52946b")
    | .primary.attributes.floating_ip
' terraform.tfstate
"10.120.241.21"

我们可以推断出您的数据看起来像

we can infer you have data which looks like

{
  "modules": [
    {
      "resources": [
        {
          "type": "openstack_compute_instance_v2",
          "primary": {
            "id": "5edfe2bf-94df-49d5-8118-3e91fb52946b",
            "attributes": {
              "name": "jumpbox"
            }
          }
        },
        {
          "type": "openstack_compute_floatingip_associate_v2",
          "primary": {
            "attributes": {
              "instance_id": "5edfe2bf-94df-49d5-8118-3e91fb52946b",
              "floating_ip": "10.120.241.21"
            }
          }
        }
      ]
    }
  ]
}

以下过滤器演示了使用功能

The following filter demonstrates a solution using functions, variables and parenthesis ():

def get_primary_id($name):
    select(.type == "openstack_compute_instance_v2"
       and .primary.attributes.name == $name)
  | .primary.id
;
def get_floating_ip($id):
    select(.type =="openstack_compute_floatingip_associate_v2"
       and .primary.attributes.instance_id == $id)
  | .primary.attributes.floating_ip
;
  .modules[]
| ( .resources[] | get_primary_id("jumpbox") ) as $id
| ( .resources[] | get_floating_ip($id)      ) as $fip
| ($id, $fip)

如果此过滤器位于filter.jq中,并且data.json包含上面的示例数据 然后

if this filter is in filter.jq and data.json contains the sample data above then

$ jq -M -f filter.jq data.json

产生输出:

"5edfe2bf-94df-49d5-8118-3e91fb52946b"
"10.120.241.21"

这篇关于jq:从另一个数组元素中按值搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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