Search code examples
jsonmulemule-esb

Mule JSON Payload null check in Choice Filter


Am I doing anything wrong here? Even the json payload is [], it's going to the default instead of Error log.

<choice doc:name="If Payload is Null">
    <when expression="#[payload == empty]">
        <mule-logger-module:logger-exception message="Null Payload" doc:name="Log Error"/>
    </when>
    <otherwise>
        <mule-logger-module:log-default message="Payload Received" type="EXIT" doc:name="Log End"/>
    </otherwise>
</choice>

Solution

  • Convert to a Array of Maps first, then use the MEL expressions to test empty:

    <json:json-to-object-transformer
                returnClass="java.util.HashMap[]" doc:name="JSON to Object" />
    <choice doc:name="If Payload is Null">
    <when expression="#[payload == empty]">
        <mule-logger-module:logger-exception message="Null Payload" doc:name="Log Error"/>
    </when>
    <otherwise>
        <mule-logger-module:log-default message="Payload Received" type="EXIT" doc:name="Log End"/>
    </otherwise>