Use jq to extract some information from a JSON file… here is a solution to the problem.
Use jq to extract some information from a JSON file
I’m extracting some information from a json file in the following format:
{
"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 jq -r .test[]. Web
output:
target1.com
target2.com
target3.com
But if I only want to get a value of type 3, that means the output will only show target2.com and target3.com
Solution
$ jq -r '.test[] | select(.type == "3"). Web' file.json
target2.com
target3.com
target3.com
This passes the .test[]
node to select
, which filters its input using the .type == "3" selector.
Then it selects . Web
。