0

We need to perform following using SSJS in Automation studio:

  1. We need to do a API call to get a XML response.
  2. We need to parse that that XML response and store values in data extension.

I am are trying to use "Script.Util.HttpGet" method to retrieve data from XML.

Whenever I use HTTPget method with HTML webpage, I get the response and I can write it using "Platform.Response.Write".

For example: Code to retrieve HTML document
"

<script runat="server">
   var req = new Script.Util.HttpGet("http://www.example.com/");
   var resp = req.send();
   Platform.Response.Write("Content:"+resp.content);
</script>

"

But Whenever I try to retrieve a data from any XML document by passing URL for XML document, it is not writing anything. How can I write to see what data it retrieved from xml?

code to retrieve XML:
"

<script runat="server">
   var req = new Script.Util.HttpGet("https://www.w3schools.com/xml/simple.xml");
   var resp = req.send();
   Platform.Response.Write("Content:"+resp.content );
</script> 

"

I am not sure how to get response from XML and parse it. I need to verify xml response and store the node values in data extension. Can someone already worked on such requirement to do a api call to get XML response and parse and store it in DE ?

Any help would be appreciated!!

Please check the following code for my example : Note, I am retrieving xml from "https://www.w3schools.com/xml/simple.xml" for this example.

<h1> Attendees Data </h1>
  <br>
  <script runat="server">
    var url = "https://www.w3schools.com/xml/simple.xml";
    var req = new Script.Util.HttpGet(url);
    var resp = req.send();
    var respString = Platform.Function.Stringify(resp.content);
    Platform.Response.Write("Response:<br/ >"+ respString +"<br />");
    Platform.Response.Write("Response String Content:<br/ >"+respString.content +"<br />");
    function trim(x) {
      return x.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
    };
    function dataFromAttr(data, attr, notNull){
      if (data){
        var reg = new RegExp("<"+attr+">", "g");
        var result = data.match(reg);
        if(result.length == 0){
          var str = '';
          return trim(str);
        }
        if(result.length == 1){
          var str = data.split("<"+attr+">")[1];
          str = str.split("</"+attr+">")[0];
          return trim(str);
        }
        if(result.length > 1){
          var str = data.split("</"+attr+"> <"+attr+">");
          return str;
        }
        var nullElementValue = new RegExp("<"+attr+" />");
        if(data.match(nullElementValue).length) return "";
        if(notNull) return "";
      }
      else{
        return null;
      }
    };
    
    var pname = dataFromAttr(respString, "name");
    var pprice = dataFromAttr(respString, "price");

    Platform.Response.Write("pname:<br/ >"+ pname +"<br />");
    
  </script>

When I stringify the response and print i.e. Platform.Response.Write("Response:<br/ >"+ respString+"<br />"); it will return the values of nodes but not nodes and properties. Check the response below after stringify:

""\r\n\r\n \r\n Belgian Waffles\r\n $5.95\r\n Two of our famous Belgian Waffles with plenty of real maple syrup\r\n 650\r\n \r\n \r\n Strawberry Belgian Waffles\r\n $7.95\r\n Light Belgian waffles covered with strawberries and whipped cream\r\n 900\r\n \r\n \r\n Berry-Berry Belgian Waffles\r\n $8.95\r\n Light Belgian waffles covered with an assortment of fresh berries and whipped cream\r\n 900\r\n \r\n \r\n French Toast\r\n $4.50\r\n Thick slices made from our homemade sourdough bread\r\n 600\r\n \r\n \r\n Homestyle Breakfast\r\n $6.95\r\n Two eggs, bacon or sausage, toast, and our ever-popular hash browns\r\n 950\r\n \r\n\r\n"

Thank You, Vikram

3
  • This could help salesforce.stackexchange.com/questions/170754/… – Jonas Lamberty Aug 13 '20 at 7:13
  • Hi Jonas, Thanks for the reply. I went through the example. But not sure what exactly passed in "Client" variable in the example provided. I tried to pass response and also the stringified response but no success. – vikram Chandane Aug 13 '20 at 10:16
  • Hi All, Is any body worked on api call to get xml response and parse it? – vikram Chandane Aug 14 '20 at 10:35
1

To write the response, you need to stringify it first, so try:

Platform.Response.Write("Content:"+Stringify(resp));

For parsing XML, there are no specific SSJS functions, but you can do it using regex.

See this thread for details: Parse XML in Server Side Javascript

7
  • Hi @zuzannamj, Thanks for the response. Stringify will print the values of nodes. But I want to print xml with all nodes. I went through the example provided i.e. "Parse XML in Server Side Javascript". But I am not able to retrieve the nodes. In the example, they passed "Client" variable to a method i.e. "dataFromAttr(client, "home_phone")" . So what would be the "Client" variable. Is it XML response or XML response after stringify. I tried passing both, but no success? Can anyone let me know what I am missing here? – vikram Chandane Aug 13 '20 at 10:09
  • Client is the name of the XML element that they wanted to display values from. Were you able to write the XML response? Can you paste the structure here? – zuzannamj Aug 13 '20 at 10:45
  • Hi zuzannamj , I updated the question with the code I have. – vikram Chandane Aug 13 '20 at 11:00
  • OK, in Eliot's example Client was the element that he wanted to retrieve information from. In your case, the element is called food and it has properties such as name, price, etc so I assume you would want to retrieve those – zuzannamj Aug 13 '20 at 11:05
  • Yes. Correct! but no success... When I stringify and print the content I can see all the values present in the nodes but not the actual nodes (XML nodes with properties) – vikram Chandane Aug 13 '20 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.