[英]mule expression for checking json file
是否可以編寫一個ule子表達式來檢查以下json請求中Name的所有值是否相同?
我正在嘗試類似#[($ in message.payload.id.items if $.Name==$.Name)]
但是它不起作用,請提出建議
{"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" } ] } }
我認為更干凈的實現方式是使用JSON到Object轉換器將JSON映射到POJO(Java對象)。
例如:
<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." />
您可以將POJO命名為“ Request”,如上例所示,並且作為成員的Items列表以及名稱為hasRepeatedItems()的布爾方法(如果重復或不重復)將返回true或false。
JSON成功通過您的轉換器之后,您的有效負載現在將成為Java對象“請求”。
您可以使用選擇路由器並調用方法payload.hasRepeatedItems()。
<choice doc:name="Choice">
<when expression="payload.hasRepeatedItems()">
Do something ...
</when>
<otherwise>
Do something else...
</otherwise>
</choice>
我希望這有幫助。 如果需要進一步說明,請讓我打結。
您可以使用以下代碼檢查所有Name元素是否都具有相同的值= '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>
備用選項:-
如果您知道JSON列表中元素的編號將是固定的(例如本例中為3 Name元素),則可以嘗試以下操作:
<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>
但是最好的選擇是遵循上述解決方案,這對於任何數量的元素都將是有益的
@Anirban ..對於每個循環解決方案都很棒..但是它的硬編碼名稱必須再次檢查整個數組的所有名稱並從中進行計數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.