JOLT规范-是否可以根据字典列表的键/值对值进行排序?



JSON输入:

{
"list": [
{
"tags": [
{
"scope": "",
"tag": "TAG_VM"
},
{
"scope": "",
"tag": "TAG_HOST"
},
{
"scope": "",
"tag": "TAG_ROLE_DNS"
},
{
"scope": "",
"tag": "TAG_ROLE_AD"
}
]
}
],
"result_count": 1,
"sort_by": "name"
}

期望输出:

{
"role1" : "DNS",
"role2" : "AD"
}

(最后一个字符串的排序值);在最后一个"_")之后

我已经试过了,但它不工作:

[
{
"operation": "shift",
"spec": {
"list": {
"*": {
"tags": {
"*": {
"tag": {
"TAG_ROLE*": "role1"
}
}
}
}
}
}
}
]

有人知道如何正确使用JOLT吗?我有点迷失了

让我们以动态的方式构建它,例如:

[
{ // extact literals from the asterisks followed by TAG_ROLE_ through going one level up and take the value of first asterisk(already exists one, but might be more than one) by using &(1 --> level up ,1 --> first asterisk)
"operation": "shift",
"spec": {
"*": {
"*": {
"*": {
"*": {
"*": {
"TAG_ROLE_*": {
"#r": "&(1,1)"
}
}
}
}
}
}
}
},
{ // build an array, namely "r" by exchanging key-value pairs, while adding a fake first component to be used for extra increment of the index value, as indexes start from zero, but we need them to start from one in the upcoming spec 
"operation": "shift",
"spec": {
"*": {
"$": "@(0)"
},
"#0": "r"
}
},
{ // we tiled the key-value pairs as desired except for the one with zero key
"operation": "shift",
"spec": {
"*": {
"*": {
"@": "role&"
}
}
}
},
{ // get rid of the first(fake) component
"operation": "remove",
"spec": {
"*0": ""
}
}
]

最新更新