Ratha
Ratha

Reputation: 9692

Reading an attribute based on a condition using dataweave 2.0 from JSON

I have a sample json as

{
  "Employees": {
    "Employee": 
       {
        "EmpID": "JC01",
        "TerminationDate": "2021-03-15",
        "FirstName" :"XXXX",
        "LastName":"aaa"
      }
  }
}

I would like to find whether "JCO1" employee is left the company or not.For that, I check the TerminationDate field and whether if it is < current date I assume the particular staff left the company.

My dataweave script to find this is;

  %dw 2.0
output application/java
---
(vars.pload.Employees.Employee filter(((if ( $.TerminationDate =='' ) '9999-01-01' else $.TerminationDate) as Date) < (now() as Date)) map($ - "TerminationDate")
)

But this fails because my payload is not an array. May i know what will be the right function to use instaed of 'filter' and 'map' here?

Upvotes: 0

Views: 150

Answers (1)

Salim Khan
Salim Khan

Reputation: 4303

One way to do would be to use .. selector to access Employee object:

%dw 2.0
output application/java
---

(payload.Employees..Employee filter(((($.TerminationDate default "9999-01-01") as Date) < (now() as Date))) map($ - "TerminationDate")
)

Another approach could be using filterObject:

%dw 2.0
output application/java
---

(payload.Employees filterObject(!((($.Employee.TerminationDate default "9999-01-01") as Date) < (now() as Date))) 
).Employee - "TerminationDate"

Upvotes: 3

Related Questions