简体   繁体   中英

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

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.

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

@Anirban ..对于每个循环解决方案都很棒..但是它的硬编码名称必须再次检查整个数组的所有名称并从中进行计数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM