简体   繁体   中英

Add Extra object inside loop in Mule Dataweave

How to add additional object inside array in dataweave. Please find the input and expected response. I stored the below input in flowVars

Input:

 {
    "calculate": [{
    "rate": 10.4500,
    "margin": 0.000,
    "amount": 1000
   }]
}

If the input amount is greater than 1000, add additional one more object along with the original one. Response should look like below

Response:

{
  "calculate": [{
    "actualRate": 10.4500,
    "amount": 1000
},
{
    "actualRate": 10.4500,
    "amount": null
  }]
}

Dataweave: (not sure how to add extra object in the response above)

 %dw 1.0
 %output application/java
 ---
 {
   calculate: flowVars.calculate map {
    actualRate:$.rate,
    amount:$.amount
 }
}

Could anyone can help me with this. Thanks in advance.

I think this could do what you need:

%dw 1.0
%output application/java
 ---
flowVars.calculate map {
       calculate:[
            { 
              actualRate:$.rate,
              amount:$.amount
             },
             {
                actualRate:$.rate,
                amount : null
             }
           ] when $.amount >= 1000
           otherwise [
                { 
                  actualRate:$.rate,
                  amount:$.amount
                 }
           ]

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM