无法在Shell脚本中使用jq获取JSON数组值
我正在尝试从下面的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将根据结果增加。
回答:
给定一个数组,您可以用来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
)
以上是 无法在Shell脚本中使用jq获取JSON数组值 的全部内容, 来源链接: utcz.com/qa/432660.html