如何从 curl json 输出中获取特定字符串
How to get a particular string from curl json output
我正在对 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 响应部分的 "key" 下):test_project-4044
有人可以帮我吗?
谢谢!
jq 是你的朋友(对于这个和许多其他 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
我正在对 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 响应部分的 "key" 下):test_project-4044
有人可以帮我吗?
谢谢!
jq 是你的朋友(对于这个和许多其他 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