我有一个json对象(从excell文件导入,之后也需要像一个一样格式化)。因此,我完全无法弄乱结构。
输入包含一个带有工作表的json格式的对象,每个工作表都是一个对象数组。
当工作表编号等于“ append”对象中的kwadrantcode时,我想向数组中添加另一个对象(var“ append”)(作为最后一项)
有效载荷
{
"(sheet)1": [
{
"kwadrantId": 1.0,
"Team": "blauw1",
"timestamp": "2019-09-26T16:37:54",
"controlecode": 12431
}
],
"(sheet)2": [
{
"kwadrantId": 2.0,
"Team": "rood1",
"timestamp": "2019-09-26T16:37:54",
"controlecode": 81243
},
{
"kwadrantId": 2.0,
"Team": "blauw2",
"timestamp": "2019-09-26T18:00:54",
"controlecode": 67676
}
]
}
%dw 2.0
output application/json
var append = {"kwadrant": "2",
"controlecode": "22222",
"kleur": "blauw",
"subteam": "1",
"form_id": "83177",
"submit": "Claim"
}
---
payload pluck($) map (sheet, sheetnumber)-> (sheet map (claim, claimcounter) -> claim) ++ [append]
此代码成功地将对象附加到数组的正确位置。 这里的问题是,最后一部分(地图目标)似乎不允许有条件的。 因此,它将始终将其插入每个列表的末尾,而不仅仅是我希望其结束的那个列表。
[
[
{
"kwadrantId": 1.0,
"Team": "blauw1",
"timestamp": "2019-09-26T16:37:54",
"controlecode": "12431"
},
{
"kwadrant": "2",
"controlecode": "22222",
"kleur": "blauw",
"subteam": "1",
"form_id": "83177",
"submit": "Claim"
}
],
[
{
"kwadrantId": 2.0,
"Team": "rood1",
"timestamp": "2019-09-26T16:37:54",
"controlecode": "12431"
},
{
"kwadrantId": 2.0,
"Team": "blauw2",
"timestamp": "2019-09-26T18:00:54",
"controlecode": 67676.0
},
{
"kwadrant": "2",
"controlecode": "22222",
"kleur": "blauw",
"subteam": "1",
"form_id": "83177",
"submit": "Claim"
}
]
]
honestly, i think i've tried everything the past two hours, but here are a few i can think of out of the top of my head.
++ [append] if (true)
(if true )++ [append]
payload pluck($) map (sheet, sheetnumber)-> (sheet map (claim, claimcounter) -> claim)((if true) ++ [append])
and most variations i can think of with brackets
我以后会担心将'append'var格式化为正确的结构,只是将其放在正确的位置是我似乎无法解决的问题。
答案 0 :(得分:6)
我想到的最简单的方法是使用mapObject函数
%dw 2.0
output application/json
var append =
{
"kwadrant": "2",
"controlecode": "22222",
"kleur": "blauw",
"subteam": "1",
"form_id": "83177",
"submit": "Claim"
}
fun extractNumber(pageName: Key) =
(pageName as String match /\(sheet\)([0-9]+)/)[1]
---
payload mapObject ((value, key, index) -> do {
if(extractNumber(key) == append.kwadrant)
{(key): value << append}
else
{(key): value}
})
因此,基本上,我将遍历根目录中的每一张表,当它与之匹配时,我需要使用 <<< / em>
将其附加到值上