0
votes

Is it possible to access data with wildcard in dataweave?

I have my payload as Entry_1_m1,Entry_2_m1,Entry_3_m1 Entries will be dynamic there might be other entry as Entry_1_m2,Entry_2_m2,Entry_3_m2. So I should be able to get the values irrespective of last number that is I should get the value of (Entry_1_m*). Is it possible in dataweave?

%dw 1.0
%input payload application/java
%output application/xml
---
{
    Employee:{
        empRequest:{
            ReqNumber:payload.RequestNumber,
            Reasons:{
                Entry:payload.Entry_1_m1,
                Entry:payload.Entry_2_m1,
                Entry:payload.Entry_3_m1
            }
        }
    }
}
1

1 Answers

1
votes

Use pluck operator to get list of keys which will help you to map all fields starting with Entry_1_m. Following code worked for me

%dw 1.0
%input payload application/java
%output application/xml
%var keys = (payload pluck $$) filter ($ startsWith 'Entry_1_m')
---
{
    Employee:{
        empRequest:{
            ReqNumber:payload.RequestNumber,
            Reasons: {(keys map {
                Entry:payload[$]
            })}
        }
    }
}

Hope this helps.