Using jq to extract specific information from a JSON file

I am extracting some information from my json files which 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 know it jq -r .test[].Web

will output:

target1.com
target2.com
target3.com 

      

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

+3


source to share


1 answer


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

      



This passes the nodes .test[]

into select

, which filters its input using a selector .type == "3"

. Then he selects .Web

from the filtered list.

+5


source







All Articles