gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:58:00 25 4
gpt4 key购买 nike

我正在对 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")



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

对我的 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 响应部分的“key”下):test_project-4044

有人可以帮我吗?

谢谢!

最佳答案

jq是你的 friend (对于这个和许多其他 json 技巧):

你不需要-D也不是 -X传递给 curl 。我想你仍然需要那个 -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

关于curl - 如何从 curl json 输出中获取特定字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59047584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com