【问题标题】:Looking for a jq filter to exclude nested objects based on the value of a nested property寻找一个 jq 过滤器以根据嵌套属性的值排除嵌套对象
【发布时间】:2017-05-02 22:19:34
【问题描述】:

鉴于此输入:

{
  "10000703": {
    "show_id": 1641788,
  },
  "10000838": {
    "show_id": 1517903,
  },
  "10001325": {
    "show_id": 1641788,
  },
}

我正在寻找一个过滤器来表示“返回所有 show_id 不等于 1641788 的对象”

预期的输出是:

{
  "10000838": {
    "show_id": 1517903,
  },
}

无法排除嵌套对象 :(

【问题讨论】:

  • 问:你试过什么?
  • 我尝试了很多涉及walk/1del() 的东西,但with_entries/1 是我所需要的。

标签: filter nested key jq


【解决方案1】:

这是 with_entries/1 的便利性和 jq 可能的简洁性的一个很好的例子:

with_entries( select(.value.show_id != 1641788 ))

with_entries/1 将对象转换为显式的 .key/.value 表示。详情请见jq manual

或者更简洁,在这种情况下也可以使用del/1

del( .[] | select( .show_id == 1641788 ) )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-11
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 2023-02-11
    相关资源
    最近更新 更多