1

Scenario - I have to iterate over this payload, and for those listings with error I would need to increment count. But how to check if error property exists?

{
    "jobGuid": "123",
    "status": "COMPLETED",
    "listings": [
        {
            "exteralListingId": 7654320
        },
        {
            "exteralListingId": 7654321,
            "error": {
                "code": "inventory.listings.sellerCreditCardNotfound",
                "description": "Seller credit card not found"
            }
        }
    ]
}

Option1 - Check using json syntax
Option2 - Iterating in a for-each loop over listings, checked for #[payload.error !=null]. But it gave error - Message payload is of type: LinkedHashMap

David Dossot
  • 33,403
  • 4
  • 38
  • 72
user3483129
  • 137
  • 6
  • 18

1 Answers1

1

You can use jsonPath something like xpath but for JSON

I attached my example with the json provided. As you can see there are #[json:listings] which return array, this array will be iterated by foreach and then validate if contains error tag using #[json:error]. errorCount variable store the number of errors and it will be printed in the console.

<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="{&quot;jobGuid&quot;:&quot;123&quot;,&quot;status&quot;:&quot;COMPLETED&quot;,&quot;listings&quot;:[{&quot;exteralListingId&quot;:7654320},{&quot;exteralListingId&quot;:7654321,&quot;error&quot;:{&quot;code&quot;:&quot;inventory.listings.sellerCreditCardNotfound&quot;,&quot;description&quot;:&quot;Seller credit card not found&quot;}},{&quot;exteralListingId&quot;:7654321,&quot;error&quot;:{&quot;code&quot;:&quot;inventory.listings.sellerCreditCardNotfound&quot;,&quot;description&quot;:&quot;Seller credit card not found&quot;}},{&quot;exteralListingId&quot;:7654321,&quot;error&quot;:{&quot;code&quot;:&quot;inventory.listings.sellerCreditCardNotfound&quot;,&quot;description&quot;:&quot;Seller credit card not found&quot;}}]}" 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>

For more information check the official documentation at mule . http://www.mulesoft.org/documentation/display/current/JSON+Module+Reference

Eddú Meléndez
  • 6,107
  • 1
  • 26
  • 35