使用 JQ 过滤嵌套对象

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

对于给定的 json:

{
  "source_1": [
    {
      "val1": "foo1",
      "val2": "bar1"
    },
    {
      "val1": "foo2",
      "val2": "bar2"
    }
  ],
  "source_2": [
    {
      "val1": "foo1",
      "val2": "bar1"
    },
    {
      "val1": "foo3",
      "val2": "bar3"
    }
  ]
}

我想递归搜索它,其中

val2
等于
bar1
,但以这种格式返回:

{
  "source_1": [
    {
      "val1": "foo1",
      "val2": "bar1"
    }
  ],
  "source_2": [
    {
      "val1": "foo1",
      "val2": "bar1"
    }
  ]
}
json jq
1个回答
1
投票

这会产生您预期的输出:

map_values(map(select(.val2 == "bar1")))

在线演示

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