【发布时间】:2019-09-25 07:35:15
【问题描述】:
我正在尝试从以下 JSON 文件中获取密钥:
我刚刚执行了下面的命令,它将给出下面的 JSON 输出
命令:
jq -r '.issues'
输出:
{
"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
预期输出:
{
"JIRA-1":"KINDLEAMZ-67578",
"JIRA-2":"KINDLEAMZ-68661",
"JIRA-3":"KINDLEAMZ-68167"
}
如何从每个数组中获取键值并像上面一样显示? JIRA-n 会根据结果增加。
当我在 shell 中运行此命令但收到此错误时。它适用于过滤器而不是外壳。
命令:
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"
错误:
/tmp/jenkins5142826499545309380.sh:命令替换:第 46 行:意外标记附近的语法错误.issues'
/tmp/jenkins5142826499545309380.sh: command substitution: line 46:$readFile |减少 (.issues | to_entries[]) 为 {$key,$value} ({}; .["JIRA-($key + 1)"] = $value.key ))'
这不是重复的,你能把它作为重复删除并帮助我得到答案吗?
【问题讨论】:
-
首先,您的 JSON 甚至对于
jq解析它都无效。您在第 6 行缺少数组开始[。修复 JSON 后,您的尝试reduce (.issues | to_entries[]) as {$key,$value} ({}; .["JIRA-\($key + 1)"] = $value.key )应该会按预期工作 -
用有效的 json 更新了问题。抱歉打错了
-
您的过滤器现在按预期工作并产生所需的输出
-
是的,工作正常...可能是 json 解析问题。
-
@inian, @oguz ismail, - 请检查更新的问题,它不重复。你能帮我解决这个问题吗?