Search code examples
javamulemule-el

mule expression for checking json file


Is it possible to write a mule expression to check whether all the values of Name in the below json request are the same?

I am trying something like #[($ in message.payload.id.items if $.Name==$.Name)] but its not working please suggest

{"id":
    {
     "items" : [
    {
    "Name": "Raj",
    "transaction_tag": "value1",
    "transaction_type": "withdraw"
    },
{
    "Name": "Raj",
    "transaction_tag": "value2",
    "transaction_type": "submit"
    },
  {
    "Name": "Raj",
    "transaction_tag": "value3",
    "transaction_type": "inquiry"
    }
]
}
}


Solution

  • I think a cleaner implementation would be to map the JSON to a POJO (Java object) using the JSON to Object transformer.

    For example:

    <json:json-to-object-transformer returnClass="com.mycompany.Request" doc:name="JSON to Request"
                doc:description="Convert the JSON payload to a Java object for further processing." />
    

    You can name your POJO "Request," as in the example above, and as a member a List of Items, and a boolean method of name hasRepeatedItems() that returns true or false if the items are repeated or not.

    After your JSON goes through your transformer (successfuly), your payload now will be a Java Object "Request."

    You can use a choice router and invoke the method payload.hasRepeatedItems().

    <choice doc:name="Choice">
                <when expression="payload.hasRepeatedItems()">
                    Do something ...
                </when>
                <otherwise>
                    Do something else...
                </otherwise>
    </choice>
    

    I hope this helps. Please let me knot if I need to elaborate further.