I have JSON request with array that needs to be sent as SOAP request in back end. Can anyone please help that how can I construct repeated occurrence in SOAP message
JSON:
{
"requestList": [{
"accountLevel": "CL",
"accountPortfolio": "01",
"account": "608859577"
}, {
"accountLevel": "CL",
"accountPortfolio": "01",
"account": "607364694"
}]
}
SOAP
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="urn:2DD4107F-4052-491E-8D41-FBDE9171A2EF">
<soap:Body>
<ns1:RequestName>
<ns1:item>
<ns1:accountLevel>CL</ns1:accountLevel>
<ns1:accountPortfolio>01</ns1:accountPortfolio>
<ns1:account>608859577</ns1:account>
</ns1:item>
<ns1:item>
<ns1:accountLevel>CL</ns1:accountLevel>
<ns1:accountPortfolio>01</ns1:accountPortfolio>
<ns1:account>607364694</ns1:account>
</ns1:item>
</ns1:RequestName>
</soap:Body>
</soap:Envelope>
I could do for 1 occurrence by parsing the JSON request in JavaScript policy and stored the request elements in variables. I used Assign Message policy to construct SOAP payload by inserting those variables.
But this logic will not work if there are multiple dynamic occurrences in request as I cannot write conditional logic in Assign Message policy.
How can I accomplish this?
FTFY. I took the liberty to reformat your code and add some things to the SOAP example.
- Your JSON was not valid, as it did not include surrounding curly braces.
- Your soap example was a snippet, as it did not include the Envelope or Body elements, both of which are required in a SOAP request.
first, use a json to xml policy on the request.content, this dynamically works on n number of array elements. Then use a javascript to replace the proper namespace on the new content. replace the existing content with new.
Here’s an example that
- uses JSONToXML to transform the original JSON into XML
- Uses XSL to transform the XML into XML with the right form and namespaces, embedded into a SOAP envelope.
For an input of
{
"requestList": [{
"accountLevel": "CL",
"accountPortfolio": "01",
"account": "608859577"
}, {
"accountLevel": "CL",
"accountPortfolio": "01",
"account": "607364694"
}]
}
The output is:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:ns1="urn:2DD4107F-4052-491E-8D41-FBDE9171A2EF"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns1:RequestElement>
<ns1:item>
<ns1:accountLevel>CL</ns1:accountLevel>
<ns1:accountPortfolio>01</ns1:accountPortfolio>
<ns1:account>608859577</ns1:account>
</ns1:item>
<ns1:item>
<ns1:accountLevel>CL</ns1:accountLevel>
<ns1:accountPortfolio>01</ns1:accountPortfolio>
<ns1:account>607364694</ns1:account>
</ns1:item>
</ns1:RequestElement>
</soap:Body>
</soap:Envelope>
…which I think is what you wanted.
apiproxy-pradeep-soap.zip
1 Like
Thanks, its perfectly working for me. I appreciate your help for such a faster resoution
You’re very welcome, I’m glad to help! Unfortunately the solution involves XSL, which is not everyone’s favorite language. But when you are dealing with XML, XSL is a good tool for the job.