Search code examples
muledataweavemulesoftmule4

Convert the string to integer or number


How can I convert the string in to the number/integer inside the transfor dataweave. I tried the below

%dw 2.0
import * from dw::util::Coercions
output application/json
---
{
   "quoteId" : vars.setQuoteOppRecIds.Id,
   "productCode" : payload.ServiceTypeCode,
   "axSequenceNumber" : vars.counter as :number,
   "phaseLevel" : payload.PhaseLevel as :number,
   "activeInSOW" : if(payload.PhaseLevelActivateInSOW == "Yes") (toBoolean("true")) else (toBoolean("false")),
   "phaseLevelProject" : payload.PhaseLevelProject
}

But I get the error like Invalid input ':', expected } or ',' for the object expression. (line 8, column 41): I tried to string to boolean using the toBoolean function and itseems to be working fine. Can anyone please tell me what is that I am missing with the string to integer/number conversion


Solution

  • The syntax for conversion in DW 2 is different. The Code you used is from dw 1. Adding references to type conversions in dw 2 below and fixed your DW script as well.

    https://docs.mulesoft.com/dataweave/2.1/dataweave-types

     %dw 2.0
    import * from dw::util::Coercions
    output application/json
    ---
    {
       "quoteId" : vars.setQuoteOppRecIds.Id,
       "productCode" : payload.ServiceTypeCode,
       "axSequenceNumber" : vars.counter as Number,
       "phaseLevel" : payload.PhaseLevel as Number,
       "activeInSOW" : if(payload.PhaseLevelActivateInSOW == "Yes") (toBoolean("true")) else (toBoolean("false")),
       "phaseLevelProject" : payload.PhaseLevelProject
    }
    

    enter image description here