【问题标题】:How to filter array of objects by element property values using jq?如何使用 jq 按元素属性值过滤对象数组?
【发布时间】:2016-11-02 11:14:42
【问题描述】:

我喜欢使用jq过滤json文件:

jq . some.json

给定包含对象数组的 json:

{
  "theList": [
    {
      "id": 1,
      "name": "Horst"
    },
    {
      "id": 2,
      "name": "Fritz"
    },
    {
      "id": 3,
      "name": "Walter"
    },
    {
      "id": 4,
      "name": "Gerhart"
    },
    {
      "id": 5,
      "name": "Harmut"
    }
  ]
}

我想过滤该列表以仅显示 id 值为 2 和 4 的元素,因此预期输出为:

{
  "id": 2,
  "name": "Fritz"
},
{
  "id": 4,
  "name": "Gerhart"
}

如何使用 jq 过滤 json?我玩过选择和地图,但没有任何一个可以工作,例如:

$ jq '.theList[] | select(.id == 2) or select(.id == 4)' array.json
true

【问题讨论】:

  • 请大家注意:问题是关于jq,而不是jQuery。
  • @T.J.Crowder YMMD ^^ 我在问题中澄清了:D

标签: json jq


【解决方案1】:

来自文档:

jq '.[] | select(.id == "second")' 

输入 [{"id": "first", "val": 1}, {"id": "second", "val": 2}]

输出 {"id": "second", "val": 2}

我认为你可以这样做:

jq '.theList[] | select(.id == 2 or .id == 4)' array.json

【讨论】:

  • jq '.theList[] | select(.id == 2) or select(.id == 4)' array.json 产生于:trueo.O
  • 它返回两个子句的评估,试试这个:jq '.theList[] | select(.id == 2 or .id == 4)'
  • jq '.theList[] | select(.id == 2 or .id == 4)' array.json 成功了,你可以更新你的答案:)
【解决方案2】:

您可以在map 中使用select

.theList | map(select(.id == (2, 4)))

或更紧凑:

[ .theList[] | select(.id == (2, 4)) ]

虽然这样写有点低效,因为表达式对于每个被比较的值都是重复的。以这种方式编写会更高效,并且可能更具可读性:

[ .theList[] | select(any(2, 4; . == .id)) ]
【解决方案3】:

在这里使用select(.id == (2, 4)) 通常效率很低(见下文)。

如果你的jq有IN/1,那么它可以用来实现更高效的解决方案:

.theList[] | select( .id | IN(2,3))

如果你的jq没有IN/1,那么你可以这样定义:

def IN(s): first(select(s == .)) // false;

效率

查看效率低下的一种方法是使用debug。例如,下面的表达式导致 10 次调用 debug,而实际上只需要 9 次相等性检查:

.theList[] | select( (.id == (2,3)) | debug )

["DEBUG:",false]
["DEBUG:",false]
["DEBUG:",true]
{
  "id": 2,
  "name": "Fritz"
}
["DEBUG:",false]
["DEBUG:",false]
["DEBUG:",true]
{
  "id": 3,
  "name": "Walter"
}
["DEBUG:",false]
["DEBUG:",false]
["DEBUG:",false]
["DEBUG:",false]

index/1

原则上,使用index/1 应该是高效的,但在撰写本文时(2017 年 10 月),它的实现虽然很快(它是用 C 编写的),但效率很低。

【讨论】:

    【解决方案4】:

    这是使用indices的解决方案:

    .theList | [ .[map(.id)|indices(2,4)[]] ]
    

    【讨论】:

      猜你喜欢
      • 2021-01-31
      • 2019-05-04
      • 1970-01-01
      • 1970-01-01
      • 2022-08-18
      • 1970-01-01
      • 1970-01-01
      • 2016-01-22
      相关资源
      最近更新 更多