在JQ中选择多个条件 [英] Selecting multiple conditionals in JQ

查看:176
本文介绍了在JQ中选择多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用jq json解析器,是否仍然可以选择多个select? 我有这个:

I've just started using jq json parser, is there anyway to choose multiple select? I have this:

cat file | jq -r '.Instances[] | {ip: .PrivateIpAddress, name: .Tags[]}
                               | select(.name.Key == "Name")'

我还需要包含.name.Key == "Type"

这是JSON:

{
  "Instances": [
    {
      "PrivateIpAddress": "1.1.1.1",
      "Tags": [
        {
          "Value": "Daily",
          "Key": "Backup"
        },
        {
          "Value": "System",
          "Key": "Name"
        },
        {
          "Value": "YES",
          "Key": "Is_in_Domain"
        },
        {
          "Value": "PROD",
          "Key": "Type"
        }
      ]
    }
  ]
}

这是当前输出:

{
  "ip": "1.1.1.1",
  "name": "System"
}
{
  "ip": "2.2.2.2",
  "name": "host"
}
{
  "ip": "3.3.3.3",
  "name": "slog"
}

所需的输出:

{
  "ip": "1.1.1.1",
  "name": "System",
  "type": "PROD"
}
{
  "ip": "2.2.2.2",
  "name": "host",
  "type": "PROD"
}
{
  "ip": "3.3.3.3",
  "name": "slog",
  "type": "PROD"
}

正确的方法是什么?谢谢.

What is the right way to do it? Thanks.

推荐答案

没有正确"的方法,但是有一些方法可以使您更轻松.

There's no "right" way to do it, but there are approaches to take that can make things easier for you.

标签已经采用了简化转换为对象(它们是对象条目)的格式.将标签转换为对象,以便于访问属性.

The tags are already in a format that makes converting to objects simple (they're object entries). Convert the tags to an object for easy access to the properties.

$ jq '.Instances[]
    | .Tags |= from_entries
    | {
        ip:   .PrivateIpAddress,
        name: .Tags.Name,
        type: .Tags.Type
      }' file

这篇关于在JQ中选择多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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