使用jq从JSON文件中提取某些信息

use*_*137 3 linux bash json jq

我从我的json文件中提取一些信息,格式如下:

{
    "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" } 
    ]
}
Run Code Online (Sandbox Code Playgroud)

我知道jq -r .test[].Web会输出:

target1.com
target2.com
target3.com 
Run Code Online (Sandbox Code Playgroud)

但是如果我只想获得类型为3的值意味着输出只显示target2.com和target3.com

Joh*_*ica 5

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

这将.test[]节点传递给select,使用.type == "3"选择器过滤其输入.然后.Web从过滤后的列表中进行选择.