JQ-如何基于数组中对象的值显示对象

问题描述 投票:0回答:2

我有一个看起来像这样的JSON文件:

{
  "InstanceId": "i-9KwoRGF6jbhYdZi823aE4qN",
  "Tags": [
    {
      "Key": "blah",
      "Value": "server-blah"
    },
    {
      "Key": "environment",
      "Value": "ops"
    },
    {
      "Key": "server_role",
      "Value": "appserver"
    },
    {
      "Key": "Name",
      "Value": "some_name"
    },
    {
      "Key": "product",
      "Value": "some_server"
    }
  ]
}
{
   ...more objects like the above...
}

我需要显示InstanceId,其中“键” ==“环境”和“值” ==“操作”。我有jq-1.6。

如果我说:

cat source.json | jq '
   { InstanceId, Tags } |
   (.Tags[] | select( .Key == "environment" ))
'

我得到了一些想要的东西,但是我无法弄清楚如何在输出中包括InstanceId,也不能弄清楚如何将select的“和”部分并入。

jq
2个回答
0
投票

我不确定这是否是您要查找的确切输出(如果不是,请注释),但这将输出包含InstanceIdTag的JSON对象的Key个[ C0]和environment Value

ops

0
投票

这是使用jq 'select( .Tags[] | (.Key == "environment" and .Value == "ops")) | .InstanceId' < source.json 的简单但有效的方法:

any
© www.soinside.com 2019 - 2024. All rights reserved.