If Else In Dataweave

 

In this tutorial, we will demonstrate if else conditional expression in dataweave

Input-1

{
    "status": "success",
    "id": 1,
    "employee_name": "Tiger Nixon",
    "employee_salary": 100000,
    "employee_age": 35
}

Input-2

{
    "status": "success",
    "id": 1,
    "employee_name": "Tiger Nixon",
    "employee_salary": 10000,
    "employee_age": 55
}

Input-3

{
    "status": "success",
    "id": 1,
    "employee_name": "Tiger Nixon",
    "employee_salary": 23000,
    "employee_age": 35
}

Input-4

{
    "status": "success",
    "id": 1,
    "employee_name": "Tiger Nixon",
    "employee_salary": 100000,
    "employee_age": 65
}

Now we will write a dataweave expression and manipulate the if the employee is eligible for discount on the basis of Input payload

Create a sample mule application in anypoint studio and configure the listener to test the application

 

Flow Control Operations In Dataweave

 

Drag and drop the transform message and write dataweave expression

%dw 2.0
output application/json
---
{
	isEligbleForDiscount: if(payload.employee_salary > 50000  and payload.employee_age <50) false
	else if(payload.employee_salary < 50000  and payload.employee_age >50) true
	else if(payload.employee_salary > 50000  and payload.employee_age >50) false
	else if(payload.employee_salary < 50000  and payload.employee_age <50) true
	else false
}

Flow Control Operations In Dataweave

 

Deploy the application and test with all the input payload

Result-1

 

Result-2

 

Result-3

 

Result-4

 

 

Mulesoft sample application: dataweave-flow-control sample

  
Thank you for taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.
 
For any latest updates or posts on our website, you can follow us on LinkedIn. Look forward to connecting with you there.


Share this:
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments