1
votes

I had json input like below

"products": {
  "p0": {
    "productId": "110",
    "productName": "bag"
  },
  "p1": {
    "productId": "160",
    "productName": "shoe"
  },
  "p2": {
    "productId": "140",
    productName": "watch"     
  }  
}

From mule I want the output like the one below

[
     {
       "productId": "110",
       "productName": "bag"
     },
     {
       "productId": "160",
       "productName": "shoe"
     },
     {
       "productId": "140",
       "productName": "watch"
     }
]

Because I need to pass the above output format to other inbound. Anyone help how to convert my input json into above output json.

Thanks in advance for all

2
Can you use DataMapper? (Mule Enterprise only)ericbn

2 Answers

0
votes

You can achieve what you want using some MEL and json transformers:

  <json:json-to-object-transformer
                returnClass="java.util.HashMap"  />

  <set-payload value="#[($.value in payload.products.entrySet())]" />

  <json:object-to-json-transformer />

If you're going to have more complex transformation, I would write a custom transformer, or script transformer using Groovys JsonBuilder and JsonSlurper or potentially Datamapper.

0
votes

If you are using Mule 3.7 and above, you can do it simply and easily with the dataweave component :-

 <flow name="Testlow">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/>
    <dw:transform-message doc:name="Transform Message">
      <dw:set-payload><![CDATA[%dw 1.0
        %output application/json
         ---
        payload.products map (
           $  
         )
       ]]></dw:set-payload>
      </dw:transform-message>
</flow>