如何使用 jq 按元素属性值过滤对象数组?

Posted

技术标签:

【中文标题】如何使用 jq 按元素属性值过滤对象数组?【英文标题】:How to filter array of objects by element property values using 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 【参考方案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)) ]

【讨论】:

巧妙使用if behavior when condition returns multiple results!【参考方案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)[]] ]

【讨论】:

以上是关于如何使用 jq 按元素属性值过滤对象数组?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用jq根据内部数组中的值过滤对象数组?

JQ:查找具有特定属性值的对象的数组索引

JavaScript对象数组如何按指定属性和排序方

对象数组按另一个数组值过滤

根据属性值用 lodash 过滤对象数组

markdown 使用ramda按嵌套值过滤对象数组