Search code examples
jsonmulestring-comparisonjsonpathmule-esb

Compare JSONPath value with flowVars value in Mule ESB


I need to compare username from JSON below to a flowVars in Mule ESB 3.8.3

{"id":"users_0001","username":"0001","firstName":"AB","lastName":"C","email":"abc@abc.com","enabled":true}

using this expression in the choice operator

<choice doc:name="Choice">
    <when expression="#[json:[0]/username != flowVars.username]">
        <flow-ref name="put account" doc:name="put account"/>
    </when>
    <otherwise>
        <flow-ref name="do nothing" doc:name="do nothing"/>
    </otherwise>
 </choice>

During debugging, I can see both json:[0]/username & flowVars.username are returning same value, but why when compare both of them it always return false?

Here's the result when I evaluate them

flowVars.username == "0001", returns true
flowVars.username == '0001', returns true
flowVars.username == 0001, returns true
json:[0]/username = 0001, returns true
json:[0]/username = "0001", returns false
json:[0]/username = '0001', returns false
json:[0]/username != flowVars.username, returns true
json:[0]/username = flowVars.username, returns false

Solution

  • I would avoid using the #[json] tool and just transform the incoming JSON into a HashMap. Your live will be easier plus is way easier to work with a HashMap.

    According to documentation: https://docs.mulesoft.com/mule-user-guide/v/3.7/mule-expression-language-tips

    JSON Processing MEL has no direct support for JSON. The json-to-object-transformer can turn a JSON payload into a hierarchy of simple data structures that are easily parsed with MEL.

    In your case, something like this:

        <json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>
        <choice doc:name="Choice">
            <when expression="#[payload.username != flowVars.username]">
                <flow-ref name="put account" doc:name="put account"/>
            </when>
            <otherwise>
                 <flow-ref name="do nothing" doc:name="do nothing"/>
            </otherwise>
        </choice>