Himanshu
Himanshu

Reputation: 24

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"
    }
]
}
}

Upvotes: 0

Views: 813

Answers (3)

Ranveer
Ranveer

Reputation: 35

@Anirban.. For each loop solution is great .. But its hard coded name you have to check all name again whole array and take count from it.

Upvotes: 0

Miguel Martinez
Miguel Martinez

Reputation: 66

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.

Upvotes: 1

Anirban Sen Chowdhary
Anirban Sen Chowdhary

Reputation: 8311

You can use the following code to check if all the Name element have same value='Raj':-

<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>

 <flow name="testFlow">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
    <json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>
        <foreach collection="#[message.payload.id.items]" doc:name="For Each">
                <choice doc:name="Choice">
                    <when expression="#[message.payload.Name=='Raj']">
                        <logger level="INFO" doc:name="Logger" message="Equal values"/>
                    </when>
                 <otherwise>
                        <logger message="Not equal" level="INFO" doc:name="Logger"/>
                    </otherwise>
                </choice>
         </foreach>
         <json:object-to-json-transformer doc:name="Object to JSON"/>
    </flow>

Alternate option:-
If you know your number of the element in the JSON list will be fixed (for example 3 Name element in this case) you can try the following :-

 <choice doc:name="Choice">
    <when expression="#[message.payload.id.items[0].Name==message.payload.id.items[1].Name and message.payload.id.items[1].Name==message.payload.id.items[2].Name and message.payload.id.items[2].Name==message.payload.id.items[0].Name]">
        <logger level="INFO" doc:name="Logger" message="Equal"/>
      </when>
      <otherwise>
         <logger message="Not equal" level="INFO" doc:name="Logger"/>
       </otherwise>
  </choice>

But the best option is to follow the above solution which will be good for any number of element

Upvotes: 0

Related Questions