json MuleSoft:更新Array中的值,并根据条件替换内容

4zcjmb1e  于 2023-01-14  发布在  其他
关注(0)|答案(2)|浏览(165)

我有下面的Payload。这里,如果点数不能被5整除,则向上舍入到最接近的5。因此,如果点数为250.2,则结果数字将为250,并且可以被5整除,因此返回的值将为250。如果结果值为251.2,则结果整数将是251,并且不能被5整除,将被上舍入为155

{
  "referenceID": "1001",
  "Year": "2023",
  "Type": "BK",
  "contracts": [
    {
      "contractId": "1",
      "contractType": "Booking",
      "Points": "250.2",
      "Reservations": {
        "reservations": [
          
        ],
        "reservationPoints": ""
      }
    },
    {
      "contractId": "1",
      "contractType": "Booking",
      "Points": "251.2",
      "Reservations": {
        "reservations": [
          
        ],
        "reservationPoints": ""
      }
    }
  ]
}

基于上述条件,输出有效载荷应如下所示

{
  "referenceID": "1001",
  "Year": "2023",
  "Type": "BK",
  "contracts": [
    {
      "contractId": "1",
      "contractType": "Booking",
      "Points": "250",
      "Reservations": {
        "reservations": [
          
        ],
        "reservationPoints": ""
      }
    },
    {
      "contractId": "1",
      "contractType": "Booking",
      "Points": "255",
      "Reservations": {
        "reservations": [
          
        ],
        "reservationPoints": ""
      }
    }
  ]
}

关于此四舍五入点到最近的数字可被5整除,我使用以下逻辑

if (((payload.contracts[0].Points as Number mod 5))<1) 
  (round((payload.contracts[0].Points as Number)/5)*5)
else 
  (ceil((payload.contracts[0].Points as Number)/5)*5)

这将根据条件获得更新的值,但我无法更新有效负载。

o4tp2gmn

o4tp2gmn1#

使用更新操作符仅更新有效负载对象中的点值。尝试如下:

%dw 2.0
output application/json
---

payload update {
    case c at .contracts -> c map ($ update{
        case p at .Points -> if((p mod 5) <1) round((p/5))*5 
        else ceil((p/5))*5
    })

}
quhf5bfb

quhf5bfb2#

基于以上建议,我更新了如下的有效载荷

%dw 2.0
    output application/json
    ---
    payload.contracts  map ((item,index)-> item  update {
            case Points at .Points -> if (((Points as Number mod 5))<1) (round((Points as Number)/5)*5)
             else (ceil((Points as Number)/5)*5) 
    })

相关问题