user3610137
user3610137

Reputation: 283

Using jq to extract certain info from JSON file

I am extracting some info from my json files that are formatted like this:

{
    "name": "value",
    "website": "https://google.com",
    "type" : "money",
    "some": "0",
    "something_else": "0",
    "something_new": "0",
    "test": [
      {"Web" : "target1.com", "type" : "2" },
      {"Web" : "target2.com", "type" : "3" },
      {"Web" : "target3.com", "type" : "3" }, 
      {"Web" : "target3.com", "type" : "3" } 
    ]
}

I am aware that jq -r .test[].Web will output:

target1.com
target2.com
target3.com 

but what if I only want to get the values with type is 3 meaning the output will only show target2.com and target3.com

Upvotes: 3

Views: 1198

Answers (1)

John Kugelman
John Kugelman

Reputation: 361547

$ jq -r '.test[] | select(.type == "3").Web' file.json 
target2.com
target3.com
target3.com

This passes the .test[] nodes to select, which filters its input using the .type == "3" selector. Then it selects .Web from the filtered list.

Upvotes: 4

Related Questions