0

I'm using SSJS to send an API request, the API response is an XML format.

For example:

<Result>
    <status>0</status>
    .
    .
    .
</Result>

How can I get the status field?

1
  • Is the API request to a SFMC SOAP object? If so, you might want to try using either the Core functions or WSProxy to make the call as both are better suited for SFMC SSJS ingestion and efficiency. – Gortonington May 20 '20 at 12:44
2

Another solution with full credit to Eliot, if you're looking for a field that only appears once you can use Regex:

Parse XML in Server Side Javascript

1
  • As a note too, with a little bit of adjustment, you can use that function to the same extent that you can use BuildRowsetFromXML. – Gortonington May 20 '20 at 14:51
3

There are no SSJS functions for parsing XML (you can only parse JSON with SSJS) but there is an AMPscript function called BuildRowSetFromXML that will do the trick. Here is an example from the ampscript.guide:

%%[

var @xml, @isXML, @nodes, @rowCount

set @xml = ""
set @xml = concat(@xml, '<?xml version="1.0" encoding="UTF-8"?>')
set @xml = concat(@xml, '<cart>')
set @xml = concat(@xml, '  <items>')
set @xml = concat(@xml, '    <item>')
set @xml = concat(@xml, '       <sku><![CDATA[123]]></sku>')
set @xml = concat(@xml, '       <name><![CDATA[Square]]></name>')
set @xml = concat(@xml, '       <url><![CDATA[https://limedash.com?sku=123]]></url>')
set @xml = concat(@xml, '    </item>')
set @xml = concat(@xml, '    <item>')
set @xml = concat(@xml, '        <sku><![CDATA[456]]></sku>')
set @xml = concat(@xml, '        <name><![CDATA[Circle]]></name>')
set @xml = concat(@xml, '        <url><![CDATA[https://limedash.com?sku=456]]></url>')
set @xml = concat(@xml, '    </item>')
set @xml = concat(@xml, '    <item>')
set @xml = concat(@xml, '        <sku><![CDATA[789]]></sku>')
set @xml = concat(@xml, '        <name><![CDATA[Triangle]]></name>')
set @xml = concat(@xml, '        <url><![CDATA[https://limedash.com?sku=789]]></url>')
set @xml = concat(@xml, '    </item>')
set @xml = concat(@xml, '  </items>')
set @xml = concat(@xml, '</cart> ')

if indexOf(@xml,"<cart>") > 0 then

  set @nodes = BuildRowsetFromXML(@xml,"/cart/items/item",0)
  set @rowCount = rowcount(@nodes)

  if @rowCount > 0 then

    for @i = 1 to @rowCount do

      var @sku
      var @name
      var @url

      set @nodepath = concat("/cart/items/item[",@i,"]/")

      if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,"sku"))) > 0 then
          set @sku = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,"sku"),0),1),'Value')
      endif

      if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,"name"))) > 0 then
          set @name = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,"name"),0),1),'Value')
      endif

      if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,"url"))) > 0 then
          set @url = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,"url"),0),1),'Value')
      endif

      if not empty(@sku) and not empty(@name) and not empty(@url) then

      ]%%

       <br><a href="%%=redirectto(@url)=%%">%%=v(@name)=%% (%%=v(@sku)=%%)</a>

      %%[

      endif

    next @i

  else

   output(concat("<br>no products found"))

  endif

else

  output(concat("<br>no XML found"))

endif

]%%

Output:

<br><a href="https://limedash.com/?sku=123">Square (123)</a>
<br><a href="https://limedash.com/?sku=456">Circle (456)</a>
<br><a href="https://limedash.com/?sku=789">Triangle (789)</a>

You can either rewrite the whole API call to AMPscript, or you can simply pass the response from SSJS to an AMPscript variable using the Variable.SetValue function: https://developer.salesforce.com/docs/atlas.en-us.mc-programmatic-content.meta/mc-programmatic-content/ssjs_utilitiesVariableSetValue.htm.

2
  • Did you set up alerts for new MC questions? 3 minutes from question to answer - WITH a complete solution for parsing XML :-o->-< You keep impressing me – Lukas Lunow May 20 '20 at 12:17
  • Credit for the solution goes solely to Adam and Eliot ;-) – zuzannamj May 20 '20 at 12:19

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.