简体   繁体   English

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

[英]Using jq to extract certain info from JSON file

I am extracting some info from my json files that are formatted like this: 我从我的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" } 
    ]
}

I am aware that jq -r .test[].Web will output: 我知道jq -r .test[].Web将输出:

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 但是如果我只想获得类型为3的值意味着输出只显示target2.com和target3.com

$ 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. 这会将.test[]节点传递给select ,它使用.type == "3"选择器过滤其输入。 Then it selects .Web from the filtered list. 然后从过滤后的列表中选择.Web

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM