Search code examples
jsongroovymulemule-studiomvel

Extract all particular field from JSON in mule esb


I have this json payload, I would want to collect all externalListingId in one shot -

{
"listings": {
    "numFound": 3,
    "listing": [
        {
            "status": "INACTIVE",
            "pricePerTicket": {
                "amount": 100,
                "currency": "USD",
            },
            "paymentType": "1",
            "externalListingId": "12208278",
            "city": "New York"
        },
        {   "status": "ACTIVE",
            "pricePerTicket": {
                "amount": 4444,
                "currency": "USD"
            },
            "paymentType": "1",
            "externalListingId": "CID1421798897102:151681733",
            "city": "Seattle"
        }
      ]
   }
}

I am using MVEL expression -

<enricher target="#[flowVars['sData']]" source="#[(externalListingId in payload.listing)]" doc:name="Message Enricher">
    <json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>
    </enricher>

But this is giving error !! - Message payload is of type: ReleasingInputStream.

Scenario - I would like to collect all externalListingId into flowVariable, convert to hashmap. I have another CSV file as input, would like to loop through that payload and check if this map contains the id!!

I am following this earlier post - Extracting array from JSON in mule esb


Solution

  • You need to transform the message's streaming payload to an object with:

    <json:json-to-object-transformer returnClass="java.lang.Object" />
    

    before the enricher, and remove the one you have inside of it.

    You also need to fix your MEL, as the listings property is missing in it:

    source="#[(externalListingId in payload.listings.listing)]"
    

    Source: http://www.mulesoft.org/documentation/display/current/Mule+Expression+Language+Tips#MuleExpressionLanguageTips-JSONProcessing