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?
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?
Another solution with full credit to Eliot, if you're looking for a field that only appears once you can use Regex:
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.