【问题标题】:jq command to filter keys based on value being > a number?jq命令根据值>数字过滤键?
【发布时间】:2016-03-10 20:11:39
【问题描述】:

我有一个简单的 JSON blob,如下所示:

{
  "thing_one": 223,
  "thing_two": 0,
  "thing_three": 0,
  "thing_four": 69,
  "thing_five": 14,
  "thing_six": 0
}

我想选择值大于 10 的键。我希望 jq 命令的输出返回一个列表,其中仅包含满足此条件的键。在上面的示例中,它将返回 "thing_one, thing_four, thing_five"。最好的方法是什么?

【问题讨论】:

    标签: json key jq


    【解决方案1】:

    类似:

    jq 'to_entries[] | select(.value > 10) | .key'
    

    【讨论】:

      【解决方案2】:
      to_entries[] | select( .value>10 ) | .key
      

      产生一个键流:

      "thing_one"
      "thing_four"
      "thing_five"
      

      使用 -r 命令行选项删除引号。

      要拓宽您的视野,请考虑以下替代方案:

      keys[] as $key
      | if .[$key] > 10 then $key else empty end
      

      使用foreach

      foreach keys[] as $key (.; .; if .[$key] > 10 then $key else empty end)
      

      至于最好的,我想你会发现上面的第二种选择是 jq 中性能最好的可能性中最简洁的。

      【讨论】:

        【解决方案3】:

        这是一个使用 delpathspaths 删除值 的解决方案

        delpaths([paths(. <= 10)])
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-06-23
          • 1970-01-01
          • 2017-04-16
          • 1970-01-01
          • 1970-01-01
          • 2018-06-19
          相关资源
          最近更新 更多