Hi Sameera,

You can use the XSLT mediator as well.


{
   "phoneNumbers":[
      {
         "number":"123",
         "person":"tt"
      },
      {
         "number":"555",
         "person":"aaa"
      }
   ]
}


convert the json payload into xml using the messageType property.
*<property name="messageType" value="application/xml" scope="axis2"/>*

You will get a message something like below.

<jsonObject>
   <phoneNumbers>
      <element>
         <number>123</number>
         <person>tt</person>
      </element>
      <element>
         <number>555</number>
         <person>aaa</person>
      </element>
   </phoneNumbers>
</jsonObject>


Now you can use the xslt mediator with a script similar to below.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:template match="/">
*<web:Payload>*
*    <web:PhoneNumbers>*
    <xsl:for-each select="//element">
    *<web:PhoneNumber>*
      *<web:Number>*<xsl:value-of select="number"/>*</web:Number>*
      *<web:Person>*<xsl:value-of select="person"/>*</web:Person>*
    *</web:PhoneNumber>*
    </xsl:for-each>
    *</web:PhoneNumbers>*
*</web:Payload>*
</xsl:template>
</xsl:stylesheet>

Result

<web:payload><web:phonenumbers><web:phonenumber><web:number>123</web:number><web:person>tt</web:person></web:phonenumber><web:phonenumber><web:number>555</web:number><web:person>aaa</web:person></web:phonenumber></web:phonenumbers></web:payload>


Best regards,
Amal.

On Fri, Jan 5, 2018 at 2:21 PM, Vinod Kavinda <[email protected]> wrote:

> Hi Sameera,
> Did you try the script mediator? Please refer SO question[1], script
> mediator has been successfully used here for a similar case.
>
> [1] - https://stackoverflow.com/questions/44817012/how-to-
> transform-json-array-response-to-xml-array-in-esb-wso2-using-script-media
>
> Regards,
> Vinod
>
> On Fri, Jan 5, 2018 at 1:51 PM, Sameera Gunarathne <[email protected]>
> wrote:
>
>> Hi,
>>
>> I'm generating a soap message from a JSON payload and there I would get
>> custom object arrays for the payload. An example message format would be
>> like below.
>>
>> {
>>    "phoneNumbers":[
>>       {
>>          "number":"",
>>          "person":""
>>       }
>>    ]
>> }
>>
>> respective soap message should be like this.
>>
>> <web:Payload>
>>     <web:PhoneNumbers>
>> <web:PhoneNumber>
>>     <web:Number></web:Number>
>>     <web:Person></web:Person>
>> <web:PhoneNumber/>
>>     </web:PhoneNumbers>
>> </web:Payload>
>>
>> I have used a payload factory mediator for the simple object
>> types(without arrays) and I use property mediator to access the object
>> fields so that I can inject them using an argument to the message template
>> used for payload factory. But when getting an array of as mentioned payload
>> the argument should be populated with following elements for the size of
>> the array.
>>
>> * <web:PhoneNumber>*
>> *     <web:Number></web:Number>*
>> *     <web:Person></web:Person>*
>> * <web:PhoneNumber/>*
>>
>> In this case, is there a way I can use payload factory with property
>> mediator? Or Is there a recommended mediators I can use for this scenario?
>>
>> Thanks and Regards,
>> Sameera,
>> --
>> Sameera Gunarathne
>> Software Engineer, WSO2 Inc. http://wso2.com
>> <http://l.facebook.com/l.php?u=http%3A%2F%2Fwso2.com&h=gAQEswASa>
>> Email: [email protected]
>> Mobile: +94714155561
>>
>
>
>
> --
> Vinod Kavinda
> Senior Software Engineer
> *WSO2 Inc. - lean . enterprise . middleware <http://www.wso2.com>.*
> Mobile : +94 (0) 712 415544
> Blog : http://soatechflicks.blogspot.com/
> [image: http://wso2.com/signature]
> <http://wso2.com/signature>
>
>


-- 
*Amal Gunatilake*
WSO2 Inc.; http://wso2.com
Lean . Enterprise . Middleware

mobile: (+94) 773561195
blog: amalg-blogs.blogspot.com; twitter: @daag123
linkedIn: https://www.linkedin.com/in/amalgunatilake
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev

Reply via email to