[英]Unable to fetch the JSON array values using jq in shell script
我正在嘗試從以下 JSON 文件中獲取密鑰:
我剛剛執行了下面的命令,它將給出下面的 JSON output
命令:
jq -r '.issues'
Output:
"issues": [
{
"expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
"id": "1999875",
"self": "https://amazon.kindle.com/jira/rest/api/2/issue/1999875",
"key": "KINDLEAMZ-67578"
},
{
"expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
"id": "2019428",
"self": "https://amazon.kindle.com/jira/rest/api/2/issue/2019428",
"key": "KINDLEAMZ-68661"
},
{
"expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
"id": "2010958",
"self": "https://amazon.kindle.com/jira/rest/api/2/issue/2010958",
"key": "KINDLEAMZ-68167"
}
]
}
我只想得到如下格式的 output 不知道如何得到它。
預期 Output:
{
"JIRA-1":"KINDLEAMZ-67578",
"JIRA-2":"KINDLEAMZ-68661",
"JIRA-3":"KINDLEAMZ-68167"
}
如何從每個數組中獲取鍵值並像上面一樣顯示? JIRA-n 將根據結果增加。
給定一個數組,您可以使用to_entries/1
到 map 數組索引和值的數組。 然后,您可以使用 map 在 object 上使用reduce
或with_entries/1
所需的鍵和值。
reduce (.issues | to_entries[]) as {$key,$value} ({};
.["JIRA-\($key + 1)"] = $value.key
)
https://jqplay.org/s/y6AFKg2dSM
.issues | with_entries({key: "JIRA-\(.key + 1)", value: .value.key})
https://jqplay.org/s/H2uxyFJn9E
看來您使用的是低於 1.5 的版本。 您需要進行一些調整並刪除解構。
reduce (.issues | to_entries[]) as $e ({};
.["JIRA-\($e.key + 1)"] = $e.value.key
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.