Dataweave(嵌套)映射条件映射

Nic*_* Aa 3 mule dataweave

案件

我有一个json对象(从excell文件导入,之后也需要像一个一样格式化)。因此,我完全无法弄乱结构。

输入包含一个带有工作表的json格式的对象,每个工作表都是一个对象数组。

当工作表编号等于“附加”对象中的kwadrantcode时,我想向数组(作为最后一项)向数组添加另一个对象(var“附加”)

输入1

有效载荷

    {
  "(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
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

到目前为止的代码

%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]
Run Code Online (Sandbox Code Playgroud)

此代码成功地将对象附加到数组的正确位置。这里的问题是,最后一部分(地图目标)似乎不允许有条件的。因此,它将始终将其插入每个列表的末尾,而不仅仅是我希望其结束的那个列表。

[
  [
    {
      "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"
    }
  ]
]
Run Code Online (Sandbox Code Playgroud)

我尝试过的几件事

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
Run Code Online (Sandbox Code Playgroud)

我将担心稍后将'append'var格式化为正确的结构,只是将其放在正确的位置是我似乎无法解决的问题。

mac*_*val 6

The simplest way that I can think of is to use the mapObject function

%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}
})
Run Code Online (Sandbox Code Playgroud)

So basically I will go for every sheet in the root and when it matches the one I need to add append it to the value using the <<