具有(简化的)Jira数据的JSON,例如:
filteredCategory () {
return this.dataToFilter
.filter((row) => row.category === this.filterCategory)
.filter((row) => row.age === this.filterKey)
}
想获得类似的输出
{
"issues": [
{
"key": "TEST-A",
"fields": { "issuelinks": [] }
},
{
"key": "TEST-B",
"fields": {
"issuelinks": [
{ "inwardIssue": { "key": "TEST-1" } },
{ "outwardIssue": { "key": "TEST-2" } },
{ "outwardIssue": { "key": "TEST-3" } }
]
}
}
]
}
尝试过(暂时忽略向内链接):
[
{ "key": "TEST-A", "inward": null, "outward": null },
{ "key": "TEST-B", "inward": ["TEST-1"], "outward": ["TEST-2", "TEST-3"] }
]
但是我得到了
cat data.json | \
jq '.issues[] | {"key":.key, "outward":.fields.issuelinks[].outwardIssue.key }'
注意:期望1){ "key": "TEST-B", "outward": "TEST-1" }
{ "key": "TEST-B", "outward": "TEST-2" }
{ "key": "TEST-B", "outward": null }
用于最后一个,2)TEST-A
和TEST-2
用于前两个,并且希望3)具有TEST-3
和{ {1}}组合成一个数组。
建议?
答案 0 :(得分:0)
让我们从您的第一次尝试开始吧:
.issues[]
| {key,
inward: .fields.issuelinks|map(.inwardIssue.key // empty),
outward: .fields.issuelinks|map(.outwardIssue.key // empty) }
以您的示例为例,它产生:
{"key":"TEST-A","inward":[],"outward":[]}
{"key":"TEST-B","inward":["TEST-1"],"outward":["TEST-2","TEST-3"]}
因此,需要进行两次修理才能达到既定目标:
(2)是可疑的,但易于实现,例如使用下面定义的帮助器功能。
def extract(f): map(f // empty) | if length ==0 then null else . end;
.issues
| map(
{key,
inward: .fields.issuelinks|extract(.inwardIssue.key),
outward: .fields.issuelinks|extract(.outwardIssue.key)})
如果extract
应保留null
和false
的值,则其定义应作相应的修改。