Tom Donnly
Tom Donnly

Reputation: 29

Retrieve value based on contents of another value

I have this json that i am trying to get the just the id out of based on a contains from another value. I am able to jq the contains part but when I add on | .id i cannot get a result

{
  "restrictions": [
    {
      "id": 1,
      "database": {
        "match": "exact",
        "value": "db_contoso"
      },
      "measurement": {},
      "permissions": [
        "write"
      ]
    },
    {
      "id": 2,
      "database": {
        "match": "exact",
        "value": "db2_contoso"
      },
      "measurement": {},
      "permissions": [
        "write"
      ]
    }
  ]
}

When id run

jq -r '.restrictions[] | .database.value | select(contains("conto")?) 

I get the values of db_contoso and db2_contoso. but I am trying to pull just the id based on that. When I add | .id to the end of that command I get nothing.

Upvotes: 0

Views: 103

Answers (1)

Inian
Inian

Reputation: 85895

So that would be to do below. Select the whole object matching the condition and get the value of .id

jq '.restrictions[] | select(.database.value | contains("conto")).id'

Upvotes: 1

Related Questions