JSON to SOAP conversion

Hi,

I Am new to APIGEE and trying a use case to transform a JSON to SOAP XML Structure, I was able to convert to XML using Json to XML transformer policy but not as specified SOAP XML. Following is an example of expected response

Ex:

Json:

{
  "id": "001",
  "username": "Sri1920",
  "Header1": "xyz",
  "Header2": "abc"
}

Desired SOAP XML:

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Header xmlns:ns1="foo">
    <ns1:header1>xyz </ns1:header1>
    <ns1:header2>abc </ns1:header2>
  </soapenv:Header>
  <soapenv:Body>
    <getUserInfo xmlns:ns1="bar" xmlns:ns2="baz">
      <ns1:id>001</ns1:id>
      <ns2:username>sri1920</ns2:username>
    </getUserInfo>
  </soapenv:Body>
</soapenv:Envelope>

How can I achieve this?

Thanks

1 Like

Hi sri1920,

To get this to work I did the following:

  1. Use an Extract variables policy to extract the values you want from the JSON payload into your own custom variables

2)Create an Assign message policy using the SET element to define the XML payload and set the verb to POST to call the SOAP endpoint. Most of the SOAP message can be hardcoded into the SET element, all you need to do is substitute your custom variables into the appropriate places in the SOAP XML above.

  1. Define your SOAP endpoint as your target endpoint.

4)For the response you may want a XML to JSON policy to pass a JSON response back to your client.

2 Likes

If I were doing this I would use an assignmessage, with some jsonpath functions.

<AssignMessage name='AM-Create-SOAP-Message'>
  <AssignVariable>
    <Name>jsonpath1</Name>
    <Value>$.id</Value>
  </AssignVariable>
  <AssignVariable>
    <Name>jsonpath2</Name>
    <Value>$.username</Value>
  </AssignVariable>
  <AssignVariable>
    <Name>jsonpath3</Name>
    <Value>$.Header1</Value>
  </AssignVariable>
  <AssignVariable>
    <Name>jsonpath4</Name>
    <Value>$.Header2</Value>
  </AssignVariable>
  <AssignVariable>
    <Name>jsoncontent</Name>
    <Ref>request.content</Ref>
  </AssignVariable>
    <Set>
        <Payload contentType="text/xml">
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <ns1:Header1 xmlns:ns1="urn:whatever1">{jsonPath(jsonpath3,jsoncontent)}</ns1:Header1>
    <ns1:Header2 xmlns:ns1="urn:whatever1">{jsonPath(jsonpath4,jsoncontent)}</ns1:Header2>
  </soap:Header>
  <soap:Body>
      <getUserInfo xmlns:ns1="urn:whatever-you-like" xmlns:ns2="urn:something-else">
        <ns1:id>{jsonPath(jsonpath1,jsoncontent)}</ns1:id>
        <ns2:username>{jsonPath(jsonpath2,jsoncontent)}</ns2:username>
      </getUserInfo>
  </soap:Body>
</soap:Envelope>
        </Payload>
        <Verb>POST</Verb>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</AssignMessage>

and the result is:

<soap:Envelope 
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <ns1:Header1 
        xmlns:ns1="urn:whatever1">xyz</ns1:Header1>
    <ns1:Header2 
        xmlns:ns1="urn:whatever1">abc</ns1:Header2>
  </soap:Header>
  <soap:Body>
    <getUserInfo 
        xmlns:ns2="urn:something-else" 
        xmlns:ns1="urn:whatever-you-like">
      <ns1:id>001</ns1:id>
      <ns2:username>Sri1920</ns2:username>
    </getUserInfo>
  </soap:Body>
</soap:Envelope>
1 Like

Thank you, @almartin and @dchiesa1

the AssignMessage and the Variable extraction I Am able to solve this