I have a json object (as imported from excell file, and it needs to be formatted like one as well afterwards). So i can not mess with the structure at all.
The input consists of a json formatted object with sheets, every sheet is an array of objects.
I want to add another object (the var 'append') to the array (as last item) when the sheetnumber equals the kwadrantcode in the 'append' object
The payload
    {
  "(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]
This code succesfully appends the object to the array at the right location. The Problem here is that this last part (the map target) does not seem to allow a conditional. So it will always insert it at the end of every list, not just the one where i want it to end up.
[
  [
    {
      "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
I will worry about formatting the 'append' var to the right structure later, just getting it in the right spot is the issue i can't seem to get working.
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}
})
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 <<
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With