检查 mule 中的 json 键或对象 属性
Check for json key or object property in mule
场景 - 我必须迭代此有效负载,对于那些有错误的列表,我需要增加计数。但是如何检查错误 属性 是否存在?
{
"jobGuid": "123",
"status": "COMPLETED",
"listings": [
{
"exteralListingId": 7654320
},
{
"exteralListingId": 7654321,
"error": {
"code": "inventory.listings.sellerCreditCardNotfound",
"description": "Seller credit card not found"
}
}
]
}
选项 1 - 使用 json 语法检查
选项 2 - 在 for-each 循环中迭代列表,检查 #[payload.error !=null]。但它给出了错误 - 消息有效负载的类型为:LinkedHashMap
您可以使用 jsonPath 之类的 xpath 但对于 JSON
我在提供的 json 中附上了示例。如您所见,有 #[json:listings]
和 return 数组,此数组将由 foreach
迭代,然后使用 #[json:error]
验证是否包含错误标记。 errorCount
变量存储错误的数量,并将打印在控制台中。
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="demoFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
<set-payload value="{"jobGuid":"123","status":"COMPLETED","listings":[{"exteralListingId":7654320},{"exteralListingId":7654321,"error":{"code":"inventory.listings.sellerCreditCardNotfound","description":"Seller credit card not found"}},{"exteralListingId":7654321,"error":{"code":"inventory.listings.sellerCreditCardNotfound","description":"Seller credit card not found"}},{"exteralListingId":7654321,"error":{"code":"inventory.listings.sellerCreditCardNotfound","description":"Seller credit card not found"}}]}" doc:name="Set Payload"/>
<expression-transformer expression="#[json:listings]" doc:name="Expression"/>
<set-variable variableName="errorCount" value="#[0]" doc:name="Variable"/>
<foreach collection="#[message.payload]" doc:name="For Each">
<expression-filter expression="#[json:error]" doc:name="Expression"/>
<set-variable variableName="errorCount" value="#[flowVars.errorCount + 1 ]" doc:name="Variable"/>
<logger message="counter: #[errorCount]" level="INFO" doc:name="Logger"/>
</foreach>
</flow>
有关更多信息,请查看 mule 上的官方文档。
http://www.mulesoft.org/documentation/display/current/JSON+Module+Reference
场景 - 我必须迭代此有效负载,对于那些有错误的列表,我需要增加计数。但是如何检查错误 属性 是否存在?
{
"jobGuid": "123",
"status": "COMPLETED",
"listings": [
{
"exteralListingId": 7654320
},
{
"exteralListingId": 7654321,
"error": {
"code": "inventory.listings.sellerCreditCardNotfound",
"description": "Seller credit card not found"
}
}
]
}
选项 1 - 使用 json 语法检查
选项 2 - 在 for-each 循环中迭代列表,检查 #[payload.error !=null]。但它给出了错误 - 消息有效负载的类型为:LinkedHashMap
您可以使用 jsonPath 之类的 xpath 但对于 JSON
我在提供的 json 中附上了示例。如您所见,有 #[json:listings]
和 return 数组,此数组将由 foreach
迭代,然后使用 #[json:error]
验证是否包含错误标记。 errorCount
变量存储错误的数量,并将打印在控制台中。
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="demoFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
<set-payload value="{"jobGuid":"123","status":"COMPLETED","listings":[{"exteralListingId":7654320},{"exteralListingId":7654321,"error":{"code":"inventory.listings.sellerCreditCardNotfound","description":"Seller credit card not found"}},{"exteralListingId":7654321,"error":{"code":"inventory.listings.sellerCreditCardNotfound","description":"Seller credit card not found"}},{"exteralListingId":7654321,"error":{"code":"inventory.listings.sellerCreditCardNotfound","description":"Seller credit card not found"}}]}" doc:name="Set Payload"/>
<expression-transformer expression="#[json:listings]" doc:name="Expression"/>
<set-variable variableName="errorCount" value="#[0]" doc:name="Variable"/>
<foreach collection="#[message.payload]" doc:name="For Each">
<expression-filter expression="#[json:error]" doc:name="Expression"/>
<set-variable variableName="errorCount" value="#[flowVars.errorCount + 1 ]" doc:name="Variable"/>
<logger message="counter: #[errorCount]" level="INFO" doc:name="Logger"/>
</foreach>
</flow>
有关更多信息,请查看 mule 上的官方文档。 http://www.mulesoft.org/documentation/display/current/JSON+Module+Reference