Am fairly new to Dataweave, trying to achieve simple if else condition based on below
if (vars.country == "USA")
{ currency: "USD" }
else { currency: "EUR" }
This works fine. However, when I am trying with other json variables as below, it fails
%dw 2.0
output application/json encoding="UTF-8"
---
Name: "ABC",
if (vars.country == "USA")
{ currency: "USD" }
else { currency: "EUR" }
A few ways to get it done:
Using a similar expression to what you have, you must enclose objects in {} when having more than one field in them
%dw 2.0
output application/json encoding="UTF-8"
---
{
Name: "ABC",
(if (vars.country == "USA")
currency: "USD"
else
currency: "EUR")
}
Using the ++ function, to concatenate objects, heres the documentation
%dw 2.0
output application/json encoding="UTF-8"
---
{Name: "ABC"} ++ (
if (vars.country == "USA")
{currency: "USD"}
else
{currency: "EUR"}
)
Finally, using the conditional elements feature
%dw 2.0
output application/json encoding="UTF-8"
---
{
Name: "ABC",
(currency: "USD") if (vars.country == "USA"),
(currency: "EUR") if not (vars.country == "USA")
}
Pick the one you like.
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