如何从curl json输出中获取特定的字符串

问题描述 投票:2回答:1

我正在针对Jira api执行以下curl命令:

issue_key = $(curl -g -D- -X GET -H“ Content-Type:application / json”“http://real_path/rest/api/2/search?jql=cf[10804]~$ {real_value}&maxResults = 2&fields = id,key”-u“ test_user:test_pass”)

我这样做是为了接收与自定义字段的特定值匹配的Jira问题密钥(cf [10804])。

对我的curl请求的响应如下:

HTTP/1.1 200
X-AREQUESTID: 674x690471x1
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Content-Security-Policy: frame-ancestors 'self'
X-ASEN: SEN-389841
Set-Cookie: JSESSIONID=blabla; Path=/; HttpOnly
X-Seraph-LoginReason: OK
Set-Cookie: blablab; Path=/
X-ASESSIONID: zffx7a
X-AUSERNAME: test_user
Cache-Control: no-cache, no-store, no-transform
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Tue, 26 Nov 2019 09:14:29 GMT

{"expand":"names,schema","startAt":0,"maxResults":2,"total":1,"issues":[{"expand":"operations,versionedRepresentations,editmeta,changelog,renderedFields","id":"574719","self":"real_path/rest/api/2/issue/574719","key":"test_project-4044"}]}

但是,我只想从curl中接收以下值(出现在响应的结尾,在JSON响应部分的“键”下:test_project-4044

有人可以帮我吗?

谢谢!

curl jq jira-rest-api
1个回答
2
投票

jq是您的朋友(为此和其他许多JSON技巧):

您不需要传递-D-X即可卷曲。我认为您仍然需要-H选项。

固定的命令行看起来像:

curl -g -H "Content-Type: application/json" "http://real_path/rest/api/2/search?jql=cf[10804]~${real_value}&maxResults=2&fields=id,key" -u "test_user:test_pass") | jq -r '.issues[0].key'

给出您问题中的示例json,并将其传递给jq -r '.issues[0].key',它将产生以下输出:

test_project-4044
© www.soinside.com 2019 - 2024. All rights reserved.