This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.

Convert a JSON Message to SOAP and SOAP to JSON V2

  1. Log in to the API Publisher and click the PhoneVerification API.
     

  2. Click the Edit link next to the API's name to go to its edit mode.
     

  3. Add the following resource to the API. 

    Tip: The resource you create here invokes the SOAP 1.2 Web service of the backend. Therefore, the recommended method is HTTP POST. As you do not include the payload in a query string, avoid giving any specific name in the URL pattern, which will be amended to the actual backend URL.

    Field Sample value
    ResourcesURL pattern/*
     Request types

    POST

  4. After the resource is added, expand it and add a parameter as follows. This parameter is used to pass the payload to the backend.

    Parameter nameDescriptionParameter TypeData Type
    bodyPass the phone number and license keybodyString

    Next, let's write a sequence to convert the JSON payload to a SOAP request. We do this because the backend accepts SOAP requests.

  5. Navigate to the Implement page and change the endpoint of the API to http://ws.cdyne.com/phoneverify/phoneverify.asmx?WSDL. Once the edits are done, click Save.

  6. Download WSO2 Developer Studio (version 3.7.1 is used here) from http://wso2.com/products/developer-studio/ and open it by double clicking the Eclipse.app file inside the downloaded folder. 

    Tip: To start Eclipse on a Mac for the first time, open a terminal and execute the following commands:

    cd <DevStudio_Home>/Eclipse.app/Contents/MacOS/
    chmod +x eclipse
    ./eclipse

    Thereafter, you can start Eclipse by double-clicking the Eclipse icon in <DevStudio_Home>.

  7. Click the Developer Studio menu and choose Open Dashboard. When the dashboard opens, click the ESB Config Project link.
  8. Create a new ESB project by the name PhoneProject.
  9. Click the Sequence link on the Developer Studio Dashboard and create a new sequence by the name  JSONtoSOAP  in the PhoneProject.
     

  10. Your sequence now appears on the Developer Studio console. From under the Mediators section, drag and drop a PayloadFactory mediator to your sequence and give the following values to the mediator.

    Tip: The PayloadFactory mediator transforms the content of your message. The <args> elements define arguments that retrieve values at runtime by evaluating the provided expression against the SOAP body. You can configure the format of the request/response and map it to the arguments. 

    For example, in the following configuration, the values for the format parameters PhoneNumber and LicenseKey will be assigned with values that are taken from the <args> elements (arguments,) in that particular order.

    For details on how you got this configuration, see PayloadFactory Mediator in the WSO2 ESB documentation.



    Payload
    <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
        <soap12:Body>
            <CheckPhoneNumber xmlns="http://ws.cdyne.com/PhoneVerify/query">
                 <PhoneNumber>$1</PhoneNumber>
                 <LicenseKey>$2</LicenseKey>
            </CheckPhoneNumber>
         </soap12:Body>
    </soap12:Envelope>
     Args

    Give the following arguments:

    TypeValueEvaluator
    expression
    //request/PhoneNumber
    xml
    expression//request/LicenseKeyxml
  11. Similarly, add a property mediator to the same sequence and give the following values to the property mediator. 

    Property NamemessageType
    Value TypeLiteral
    Valueapplication/soap+xml
    Property Scopeaxis2
  12. Save the sequence, which is in XML format (e.g., JSONtoSOAP.xml). This will be the In sequence for your API.

  13. Go back to the Developer Studio Dashboard, click the Sequence link and create another sequence by the name  SOAPtoJSON  in the PhoneProject.
     

  14. Add a property mediator to the second sequence and give the following values to the property mediator. 

    Property NamemessageType
    Value TypeLiteral
    Valueapplication/json
    Property Scopeaxis2

  15. Save the sequence, which is in XML format (e.g., SOAPtoJSON.xml). This will be the Out sequence for your API.

  16. Log in to the API Gateway's management console. If you are using WSO2 Cloud, the Gateway URL is  https://gateway.api.cloud.wso2.com/carbon/admin/login.jsp . If you are using a local setup, the URL is  https://localhost:9443/carbon . You can see the username on the top right-hand corner of the API Publisher.
  17. After logging in, click the Browse menu under the Resources menu. 
     

  18. When the API Gateway's registry opens, navigate to the registry path  /_system/governance/apimgt/customsequences/in . This is because you want the custom sequence to be invoked in the In direction or the request path. 
  19. Click Add Resource and upload the XML file of the sequence that you created earlier.
     
    Next, let's write another sequence to convert the SOAP response that the backend sends to JSON. 

  20. Similarly, navigate to the registry path  /_system/governance/apimgt/customsequences/out and upload the SOAPtoJSON.xml sequence file. This will invoke the second sequence in the Out direction or the response path. 
  21. Log back to the API Publisher, click the Edit link associated wit the API, navigate to the Manage tab, click the Sequences check-box and engage the In and out sequences that you created earlier.
     
  22. Save the API.
    You have created an API, a resource to access the SOAP backend and engaged sequences to the request and response paths to convert the message format from JSON to SOAP and back to JSON. Let's subscribe to the API and invoke it.
  23. Log in to the API Store and subscribe to the API and create an access token if you have not done so already.

  24. Go to the API Console tab and expand the POST method.

  25. Give the payload in the body parameter in JSON format and click Try it out. Here's a sample JSON payload: {"request":{"PhoneNumber":"18006785432","LicenseKey":"0"}}
     

  26. Note that you get a JSON response to the JSON request whereas the backend accepts SOAP messages. The request and response are conve rted by the sequences that you engaged at the API Gateway.
     

In this tutorial, you converted a message from JSON to SOAP and back to JSON using In and Out sequences.