通过嵌套数组值的过滤来过滤jq中的对象

时间:2017-12-19 12:54:31

标签: jq

给出这样的文件:

[
  {
    "KVs" : [
      {
        "Key": "animal",
        "Value": "lion"
      },
      {
        "Key": "mascot",
        "Value": "lion"
      }
    ],
    "name": "roger"
  },
  {
    "KVs" : [
      {
        "Key": "animal",
        "Value": "zebra"
      },
      {
        "Key": "mascot",
        "Value": "lion"
      }
    ],
    "name": "linda"
  }
]

我想使用jq仅选择顶部数组中包含KV对animal == "lion"的那些元素。

上述JSON文档的输出应为:

{
  "KVs" : [
    {
      "Key": "animal",
      "Value": "lion"
    },
    {
      "Key": "mascot",
      "Value": "lion"
    }
  ],
  "name": "roger"
}

无法弄清楚如何使用select()完成此操作。我知道如何使用它来根据一个特定的字段进行选择。例如按键名称:.[] | select(.KVs[].Key == "animal"),对吧?但是如何告诉它在两个字段(Key& Value)上匹配相同的KV对象?

1 个答案:

答案 0 :(得分:3)

NM,在jqplay的帮助下解决了这个问题,并进行了一些反复试验。

这是解决方案: :ENC(<encypted-val>)

相关问题