3

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

Community
  • 1
  • 1
user3483129
  • 137
  • 5
  • 17

2 Answers2

5

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

David Dossot
  • 33,350
  • 4
  • 35
  • 71
  • After adding the json to object transformer before enricher, I am getting error - Message payload type LinkedHashMap, i guess I can no longer use MVEL expression to gather all externalListingId – user3483129 Mar 16 '15 at 21:28
  • Sorry, I forgot to tell you to remove the transformer from within the `enricher`. And, no, you can totally use MEL to gather the value, that's the whole point of doing the JSON transformation. You actually have an issue in your MEL. I've edited my answer. – David Dossot Mar 16 '15 at 21:49
  • 1
    Great! got it working. In a expression filter I am checking #[flowVars['sData'].contains(payload.TicketID)]. I actually want if not contains then go-ahead condition – user3483129 Mar 16 '15 at 22:24
0

it worked with the same definition.

<flow name="test-mule-json-extractFlow"> <http:listener config-ref="HTTP_Listener_Configuration" path="/json" doc:name="HTTP"/> <enricher source="#[(externalListingId in payload.listings.listing)]" target="#[flowVars.ids]" doc:name="Message Enricher"> <json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/> </enricher> <logger message=":::::::::::::::#[flowVars.ids]" level="INFO" doc:name="Logger"/> </flow>

Note: the json input you mentioned is not valid due to available of an extra comma.

Mohan
  • 520
  • 4
  • 16
  • What is the message payload type after the `enricher`? Is it a `Map` or an `InputStream`? If a stream, then it's been consumed and is useless. In any case, it's clearer to have the transformation before the `enricher` to make clear what's the new payload type. In any case, I'm curious to know: what's the payload type after the `enricher`? – David Dossot Mar 17 '15 at 05:30
  • Hi David,the payload type after enricher was BufferInputStream. – Mohan Mar 17 '15 at 05:39
  • Thanks Mohan. So it would be consumed by the transformer inside the `enricher` and thus rendered useless (using it will throw an exception). Thus I stay on my recommendation to place the transformer before the `enricher` so the payload becomes a `Map` and remains usable. – David Dossot Mar 17 '15 at 13:51