0

How would I aggregate data (java.util.Map) format to construct a JSON payloads. Eg -

"listings": [
   {
       "listingId":"7676762734623",
       "section": "section-100",
       "products": 
       [

           {"row":"A4","seat":"11"}
       ]
   },
  ... 100 more such listings arrays
user3483129
  • 137
  • 6
  • 18

2 Answers2

0

You can use groovy in scripting:component and the use a json transformer or use a Custom Transformer. See this example:

<scripting:component doc:name="Groovy">
       <scripting:script engine="Groovy"><![CDATA[
            Map<String, Object> map1 = new HashMap<String, Object>();
            map1.put("fill","none");
            map1.put("stroke","#fff");  
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("content", map1);
            return map;]]></scripting:script>
</scripting:component>
<json:object-to-json-transformer doc:name="Object to JSON"/>
Eddú Meléndez
  • 6,107
  • 1
  • 26
  • 35
  • Hi, I have {"eventId":"9034782","externalListingId":"12208279","quantity":"2","section":"Upper Box 527","pricePerProduct":"35.785","products":["{row: 1,seat: 1}","{row: 1,seat: 2}"]} . I want to add Listing as head element – user3483129 Feb 18 '15 at 20:10
  • I think that this json is the result from a REST service. So, this json (result) convert to a map using then add the listing as a head element and use in order to get the final json. – Eddú Meléndez Feb 18 '15 at 20:16
  • I am using JSONSlurper to achieve this. However I cannot pass a variable to it. eg - def s= "new" ; slurper.parseText('s') is giving an error!! – user3483129 Feb 19 '15 at 23:54
  • I think that you can modify your question in order to have all the details. What do you really want to do? – Eddú Meléndez Feb 20 '15 at 01:47
  • How would I add variables to sluper object be parsed later - eg- def s = "new" def slurper = new JsonSlurper() def result = slurper.parseText('"listings":{"status":s}') This is giving an error! How can I also append list instead of string – user3483129 Feb 20 '15 at 19:28
  • I realized that you are using groovy component in mule flow. The final result in each step is stored in payload, you should work with this object. – Eddú Meléndez Feb 20 '15 at 19:42
-1

You can write a Java component that aggregates data to a Map and convert that to a JSON message. Please go through this SO link

Community
  • 1
  • 1
Ram Bavireddi
  • 1,139
  • 1
  • 19
  • 43