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?
Solved! Go to Solution.
Here's an example that
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.
FTFY. I took the liberty to reformat your code and add some things to the SOAP example.
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
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.
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.
User | Count |
---|---|
1 | |
1 | |
1 | |
1 | |
1 |