Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用jq提取此JSON片段中href的值?_Json_Filtering_Jq - Fatal编程技术网

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

如何使用jq提取此JSON片段中href的值?,json,filtering,jq,Json,Filtering,Jq,我有一个返回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"

我有一个返回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"
    }
  ]
}
我希望能够解析值
https://www.mycompany.com/api/nodes/node-aa5a5a55
来自
链接
数组,其中
rel
值为
节点
。如何构造一个
jq
过滤器来实现这一点?

如何:

jq -r '.links | map(select(.rel="node") | .href ) | first'
我发现这是可行的
jq-r'.links[]| select(.rel==“node”).href'

我不确定与Hans的答案相比有哪些利弊,我还发现这同样有效。
jq-r'.links[]| select(.rel | test(“node”)。href'
,一个比另一个好的原因是什么?似乎首先使用
可以处理
链接
数组中可能有多个对象的情况,其中
rel=node
,在这种情况下,它只会返回一个结果,但在我的情况下,API只会返回一个与此条件匹配的对象,您不需要要使用
test
只需使用直接字符串比较,
.links[]| select(.rel==“node”)。href