How to filter array of objects by element property values using jq?

后端 未结 4 585
鱼传尺愫
鱼传尺愫 2020-12-01 20:35

I like to filter json files using jq:

jq . some.json

Given the json containing an array of objects:

{
  \"theList\": [
             


        
4条回答
  •  囚心锁ツ
    2020-12-01 21:08

    You could use select within map.

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

    Or more compact:

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

    Though written that way is a little inefficient since the expression is duplicated for every value being compared. It'll be more efficient and possibly more readable written this way:

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

提交回复
热议问题