How to check for empty array inside a payload ?

Sandeep Deshmukh picture Sandeep Deshmukh · Jun 8, 2018 · Viewed 8k times · Source

I am trying to have a choice condition where i will run a flow depending if array is empty.

Example :

Input : {"data":{"detailsSearch":[]}}

My code :

1. #[payload.data.detailsSearch*.size() > 0]
2. #[payload.data.detailsSearch*?]

I am getting an Runtime exception :

org.mule.api.MessagingException: Execution of the expression "payload.data.detailsSearch*.size() > 0" failed. (org.mule.api.expression.ExpressionRuntimeException).

I am looking for snippet to check for empty and null for this array

Answer

Ryan Carter picture Ryan Carter · Jun 9, 2018

In Mule 4 you can use Dataweave expressions in a choice router. If using Mule 4, you can use Dataweave 2.0 syntax and the sizeOf function:

<choice>
    <when expression="#[(sizeOf(payload.data.detailsSearch)) > 0]">
        ...     
    </when>
</choice>

Otherwise if you are using Mule 3, you can just use MEL and java syntax. Like so:

...