我无法使用 Jolt Transform 获得所需的输出。
我的输入 JsonArray 看起来像这样:
[
{
"from": [
{
"area1": 1
},
{
"area2": 1
},
{
"area3": 1
}
],
"id": 111,
"to": "destination1"
},
{
"from": [
{
"area1": 2
},
{
"area2": 2
},
{
"area3": 2
}
],
"id": 222,
"to": "destination2"
}
]
我正在使用此 Jolt 规范为 JsonArray“from”中的每个 JsonObject 创建一个 JsonArray,并希望包含“id”和“to”键:
[
{
"operation": "shift",
"spec": {
"*": {
"from": {
"*": {
"@": "[&]",
"@(2,id)": "[&].id",
"@(2,to)": "[&].to"
}
}
}
}
}
]
这是输出:
[ [ {
"area1" : 1,
"id" : 111,
"to" : "destination1"
}, {
"area1" : 2
} ], [ {
"area2" : 1,
"id" : 111,
"to" : "destination1"
}, {
"area2" : 2
} ], [ {
"area3" : 1,
"id" : 111,
"to" : "destination1"
}, {
"area3" : 2
} ] ]
我想要的输出看起来像这样:
[ {
"area1" : 1,
"id" : 111,
"to" : "destination1"
}, {
"area1" : 2,
"id" : 222,
"to" : "destination2"
}, {
"area2" : 1,
"id" : 111,
"to" : "destination1"
}, {
"area2" : 2
"id" : 222,
"to" : "destination2"
}, {
"area3" : 1,
"id" : 111,
"to" : "destination1"
}, {
"area3" : 2
"id" : 222,
"to" : "destination2"
} ]
我不确定为什么输入的第二个 JsonObject 中不包含“id”和“to”键。 有谁知道如何修复 Jolt 以获得所需的输出?
首先,不需要一一写外层属性
我建议将以下 shift 作为第一个规范,如果最外层数组中只有一个嵌套对象:
{
"operation": "shift",
"spec": {
"*": {
"from": {
"*": {
"@2": {
"*": "[&1].&"
},
"*": "[&1].&"
}
}
}
}
}
但是因为你有两个,所以需要将它们分开,为此:分别在标识符前面加上 &4 和 &3 前缀,如以下完整转换所示:
[
{
"operation": "shift",
"spec": {
"*": {
"from": {
"*": {
"@2": {
"*": "&4[&1].&"
},
"*": "&3[&1].&"
}
}
}
}
},
{ // get rid of the generated inner "from" arrays
"operation": "remove",
"spec": {
"*": {
"*": {
"from": ""
}
}
}
},
{ // get rid of the object keys
"operation": "shift",
"spec": {
"*": {
"*": ""
}
}
}
]
网站上的 演示 https://jolt-demo.appspot.com/ 是: