bat_singer
bat_singer

Reputation: 11

filter json using jq and get entire other value

I'm trying to filter the following json object to get the id which doesn't have a domain value which is either "moderator" or "owner" using jq in bash script.The problem is my third domain value is random and not predetermined. How do I filter it?

{
 "data":[
   {
      "id":1,
      "domain":"moderator"
   },
   {
      "id":2,
      "domain":"owner"
   },
   {
      "id":3,
      "domain":"34b5756175a848f7a1395e1a19e10602"
   }
 ]
}

Upvotes: 1

Views: 901

Answers (2)

redInk
redInk

Reputation: 725

You can use the jq function select

cat text.json | jq '.data[] | select((.domain != "moderator") and (.domain != "owner"))'

Also, you can use the length function to get the size of the array.

jq '.[] | length' test.json

Upvotes: 0

Shawn
Shawn

Reputation: 52354

You can get just the elements of the array with a different domain via:

$ jq '.data[] | select(.domain != "moderator" and .domain != "owner")' input.json
{
  "id": 3,
  "domain": "34b5756175a848f7a1395e1a19e10602"
}

If you just want the id value and not the entire object, add | .id to the end of the jq filter.

Upvotes: 1

Related Questions