Unable to convert the json to xml

AM using a javascript to make two concurrent calls and once i get the response am merging them and all is done in javascript, I've written another one javascript after this, and am setting request.content to that variable containing JSON. Now i want to convert it to XML, but that's not happening. Could you please help?

0 2 635
2 REPLIES 2

Hi @Aiyappa M B, it is hard to tell without seeing the code or flows. If possible please upload a sample proxy or some code/image to understand the flow.

Btw if you are trying to convert final response from JSON to XML then you need to use the JSON to XML policy on the response flow after the second Javascript policy?

Did you check the Trace tab for any issue?

Where are you using the JS policies in the flow?

I agree with Siddharth... It's impossible to know what's going wrong with the details you're providing.

Now i want to convert it to XML, but that's not happening.

Yes, I'd suggest that you BREAK DOWN the problem.

We know that the JSONToXML policy is very broadly used. We know it works. OK, so the policy is known to be good.

In which cases does JSONToXML NOT convert JSON to XML? In my experience, these are the most common cases.

  • When the inbound content is not JSON. Are you sure your "new variable" is actually JSON? Could it be that it is somehow invalid - missing a comma? uses single quotes instead of double? Doesn't quote property names? Uses semicolon instead of comma?
  • When the source is not a MESSAGE. The Source element must denote a message. Like request, response, or a synthetic message you have created using AssignMessage. or with Javascript (var myRequest = new Request(); )
  • When the source message does not include a content-type header, or when the content-type header is not application/json

The first issue is easy. If you are creating a string in JavaScript, JSON.stringify() will give you a legal JSON payload. But the latter two issues are more surprising, and are often overlooked.

It may be a little disappointing that we need to assign a Message, just to act as a carrier or container for a string that we'd like to convert from JSON to XML. I understand that.

But for now, that's how it works.