How to convert XmlToJson and JsonToXml reponse, through javascipt or javacallout policy.

vermagovind92
Participant III

Hi All,

I have a usecase where to send xml request and get the json response and vice-versa,

Could someone please provide me with samples on this?

Note : I don't want to perform this task using XMLTOJSON and XMLTOJSON policy.

If my xml request is:

<employees>
<employee>
<firstName>Govind</firstName><lastName>Verma</lastName>
</employee>
<employee>
<firstName>Amit</firstName><lastName>Kumar</lastName>
</employee>
</employees>

then i got json response as :

{"employees":[
{ "firstName":"Govind", "lastName":"Verma" },
{ "firstName":"Amit", "lastName":"Kumar" }
]}

And similarly vice-versa for this.(json to xml).

Thanks & Regards

Govind

@Dino, @Anil Sagar @ Google

0 4 223
4 REPLIES 4

robert
Participant V

Between the two, recommend using the Java Callout policy. It's pretty straightforward but you need to leverage a library to do it. There are also some differences between XML and JSON and you will need to account for that. Here's one library that converts between them: https://github.com/stleary/JSON-java

If i want to perform same task by the help of "JavaScript Policy", Then could you please provide me with samples for this?

Hi,

I think maybe you asked this question previously. And ... on that previous question, I asked "WHY do you want to rule out the use of the built-in policies, which were designed for this purpose?"

I don't remember seeing your answer for that. Can you explain?

Hi Thanks for your reply ,

This is the requirement i got , where i need to convert the Xml request to Json response and Json request to Xml response with the help of JavaScript. Sample scenario i mentioned in my question. could you please provide me with samples for this or any reference, i have been used the StackOverFlow step, but unfortunately it's doesn't works for me. And i have been used the TagetEndpoint and NoTarget both but in PostMan i'm getting error.