[英]Unable to fetch the JSON array values using jq in shell script
I'm trying to get the Key from the below JSON file:我正在尝试从以下 JSON 文件中获取密钥:
I just executed the below command which will give the below JSON output我刚刚执行了下面的命令,它将给出下面的 JSON output
Command:命令:
jq -r '.issues'
Output: 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"
}
]
}
I just want to get the output as below format and not sure how to get it.我只想得到如下格式的 output 不知道如何得到它。
Expected Output:预期 Output:
{
"JIRA-1":"KINDLEAMZ-67578",
"JIRA-2":"KINDLEAMZ-68661",
"JIRA-3":"KINDLEAMZ-68167"
}
How can I get key value from each of the array and display like above?如何从每个数组中获取键值并像上面一样显示? and JIRA-n will be increase based on the result. JIRA-n 将根据结果增加。
Given an array, you can use to_entries/1
to map the array an array of index and values.给定一个数组,您可以使用to_entries/1
到 map 数组索引和值的数组。 You could then map out to the keys and values you want on the object either using reduce
or with_entries/1
.然后,您可以使用 map 在 object 上使用reduce
或with_entries/1
所需的键和值。
reduce (.issues | to_entries[]) as {$key,$value} ({};
.["JIRA-\($key + 1)"] = $value.key
)
https://jqplay.org/s/y6AFKg2dSM https://jqplay.org/s/y6AFKg2dSM
.issues | with_entries({key: "JIRA-\(.key + 1)", value: .value.key})
https://jqplay.org/s/H2uxyFJn9E https://jqplay.org/s/H2uxyFJn9E
It seems like you're using a version lesser than 1.5.看来您使用的是低于 1.5 的版本。 You'll need to make some adjustments and remove the deconstruction.您需要进行一些调整并删除解构。
reduce (.issues | to_entries[]) as $e ({};
.["JIRA-\($e.key + 1)"] = $e.value.key
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.