Kingalione
Kingalione

Reputation: 4265

jq filter objects with specific values and get Id of parent object

Hi I'm playing a bit with jq and have a problem.

This is my data:

{
    "auctionInfo": [{
            "tradeId": 229143000306,
            "itemData": {
                "id": 320854114832,
                "rating": 82
            }
        },
        {
            "tradeId": 229143000307,
            "itemData": {
                "id": 320854114833,
                "rating": 84
            }
        },
        {
            "tradeId": 229143000308,
            "itemData": {
                "id": 320854114834,
                "rating": 84
            }
        }
    ]
}

What I now want are alle tradeIds where the rating is 84.

So I tried it with this filter:

| jq -r .auctionInfo[].itemData | select(.rating==84)

But with this filter I'm inside the itemData and have no access to the tradeId which I'm interested for. I'm not really familiar with jq so might be a basic question but how can I reach the tradeId from where I'm standing with this filter?

Upvotes: 3

Views: 507

Answers (1)

Inian
Inian

Reputation: 85693

I think all you need to do is below. The logic is you need to list all array objects, filter on the rating value to get the subset and print the tradeId for those objects

jq '.auctionInfo[] | select(.itemData.rating == 84).tradeId'

Upvotes: 3

Related Questions