我试图从下面的 JSON 文件中获取密钥:
我刚刚执行了下面的命令,它将给出下面的 JSON 输出
Command:
jq -r '.issues'
Output:
{
"expand": "schema,names",
"startAt": 0,
"maxResults": 50,
"total": 4,
"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"
}
]
}
我只想获得以下格式的输出,但不知道如何获得它。
https://jqplay.org/s/0IfiBoskG5 https://jqplay.org/s/0IfiBoskG5
预期输出:
{
"JIRA-1":"KINDLEAMZ-67578",
"JIRA-2":"KINDLEAMZ-68661",
"JIRA-3":"KINDLEAMZ-68167"
}
如何从每个数组中获取键值并像上面一样显示? JIRA-n 将根据结果增加。
当我在 shell 中运行此命令但出现此错误时。它在过滤器中起作用,而不是在外壳中起作用。
Command:
sudo apt-get update
sudo apt-get install jq
readFile=$(cat response.json)
echo "$readFile" // It contains the above JSON file that mentioned as output
getResponse=$($readFile | reduce (.issues | to_entries[]) as {$key,$value} ({}; .["JIRA-\($key + 1)"] = $value.key ))
echo "$getResponse"
Error:/tmp/jenkins5142826499545309380.sh:命令替换:第 46 行:意外标记附近的语法错误.issues'
/tmp/jenkins5142826499545309380.sh: command substitution: line 46:
$readFile | 读文件reduce (.issues | to_entries[]) as {$key,$value} ({}; .["JIRA-($key + 1)"] = $value.key ))'
这不是重复的,您能否将其删除为重复并帮助我获得答案?