我正在尝试从下面的JSON文件中获取密钥:
我刚刚执行了以下命令,它将给出以下JSON输出
命令:
jq -r '.issues'
输出:
"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"
}
]
}
我只想获取以下格式的输出,不确定如何获取。
预期输出:
{
"JIRA-1":"KINDLEAMZ-67578",
"JIRA-2":"KINDLEAMZ-68661",
"JIRA-3":"KINDLEAMZ-68167"
}
如何从每个数组中获取键值并显示如上?并根据结果增加JIRA-n。
答案 0 :(得分:1)
给定一个数组,您可以使用to_entries/1
将该数组映射为索引和值的数组。然后,您可以使用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
)