简体   繁体   中英

Dataweave - filter an array and extract only one element which is not null

I am a newbie to Dataweave - am using mule 3 so the DWL is 1.0 here is the payload :

{
"students": [
    {
        "name": "Jack",
        "dob": "2022-01-01",
        "lname": "Montana"
    },
    {
        "name": "Jane",
        "lname": "Montana"
    }
]   
}

So in the above example - from the json payload of students ( array ) I want to extract only the value of 'dob' . ( Edit 1 : array can contain more than one element having dob , some elements may not have dob but there will be atleast one element in array containing dob ) Also from the array I only need one value of dob whichever is first found . In above example I need to extract the value : 2022-01-01

If more than one dob value is present in array , simply extract the first found value

I am a little stumped on how to go about it Have tried the following :

%var studentDobArr= payload.students
---
studentDobArr.dob

However this is returning an array and also returns all the elements where dob is present I somehow need to check if dob is present and if present return only one single value

You were nearly there. payload.students.dob[0] will return the first 'dob' element or null if there is no dob attribute in any students elements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM