1

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

trx
  • 2,077
  • 9
  • 48
  • 97

1 Answers1

3

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

  • Hey thank you so much.. Can you please suggest how I can convert the values to Integer for the fields with decimal value ` "unitPrice": payload.UnitPrice as Integer ` and it throws error like `Unable to resolve reference of: `Integer`.` – trx Mar 29 '22 at 17:50
  • Can you share the input value you are referring to here – Aditya Gundamaneni Mar 29 '22 at 18:00
  • ""Cannot coerce String (2,854.50) to Number 16| "salesPriceCustomer": – trx Mar 29 '22 at 18:02
  • I have the 2,854.50 as string I wanted to convert it to the Integer but that where it is throwing the error – trx Mar 29 '22 at 18:03
  • 1
    This DW will work, i tried with multiple combinations and tested it in DW playground. `payload.UnitPrice as Number {format: "#,#.##"}` **upvote if it helps** – Aditya Gundamaneni Mar 29 '22 at 18:09