简体   繁体   English

如何使用jq提取此JSON代码段中href的值?

[英]How can I use jq to extract the value of href in this JSON snippet?

I have an API returning JSON similar to the sample below 我有一个返回JSON的API,类似于下面的示例

{
  "href": "https://www.mycompany.com/api/services/myservice/instances/service-instance-3",
  "id": "si-555aa555a555",
  "links": [
    {
      "rel": "node",
      "href": "https://www.mycompany.com/api/nodes/node-aa5a5a55"
    }
  ]
}

I want to be able to parse the value https://www.mycompany.com/api/nodes/node-aa5a5a55 from the links array where the rel value is node . 我希望能够从rel值为nodelinks数组中解析值https://www.mycompany.com/api/nodes/node-aa5a5a55 How can I construct a jq filter to achieve this? 如何构造jq过滤器以实现此目的?

怎么样:

jq -r '.links | map(select(.rel="node") | .href ) | first'

I found that this works jq -r '.links[] | select(.rel == "node") .href' 我发现这有效jq -r '.links[] | select(.rel == "node") .href' jq -r '.links[] | select(.rel == "node") .href' I'm not sure of the pros/cons vs Hans' answer jq -r '.links[] | select(.rel == "node") .href'我不确定汉斯的利弊

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

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