Convert XML to JSON using XSL in Salesforce Marketing Cloud

XML to JSON

Code snippet #1: XSL File

Code snippet #2: Sample XML

%%[
VAR @xml
set @xml = ''
set @xml = concat(@xml,'<?xml version="1.0"?>')
set @xml = concat(@xml,'<loyaltyRewards>')
set @xml = concat(@xml,' <emailAddress><![CDATA[suzy@limedash.com]]></emailAddress>')
set @xml = concat(@xml,' <firstName><![CDATA[Suzy]]></firstName>')
set @xml = concat(@xml,' <rewards>')
set @xml = concat(@xml,' <reward>')
set @xml = concat(@xml,' <rewardDate><![CDATA[2017-10-15]]></rewardDate>')
set @xml = concat(@xml,' <name><![CDATA[Purchase]]></name>')
set @xml = concat(@xml,' <description><![CDATA[Purchase cash back]]></description>')
set @xml = concat(@xml,' <amount><![CDATA[3.21]]></amount>')
set @xml = concat(@xml,' </reward>')
set @xml = concat(@xml,' <reward>')
set @xml = concat(@xml,' <rewardDate><![CDATA[2017-10-01]]></rewardDate>')
set @xml = concat(@xml,' <name><![CDATA[Visit]]></name>')
set @xml = concat(@xml,' <description><![CDATA[October visit reward]]></description>')
set @xml = concat(@xml,' <amount><![CDATA[12.34]]></amount>')
set @xml = concat(@xml,' </reward>')
set @xml = concat(@xml,' <reward>')
set @xml = concat(@xml,' <rewardDate><![CDATA[2017-12-01]]></rewardDate>')
set @xml = concat(@xml,' <name><![CDATA[Referral]]></name>')
set @xml = concat(@xml,' <description><![CDATA[Referral bonus]]></description>')
set @xml = concat(@xml,' <amount><![CDATA[43.21]]></amount>')
set @xml = concat(@xml,' </reward>')
set @xml = concat(@xml,' </rewards>')
set @xml = concat(@xml,'</loyaltyRewards>')
]%%%%=v(@xml)=%%

Email Code

%%[
SET @XML = Trim(ContentBlockByID("xxxx"))
SET @XSLT = ContentBlockByID("xxxx")
SET @JSON = TransformXML(@XML, @XSLT)
]%%
<table border="1" width="600" cellspacing="0" cellpadding="15">
<tr>
<td width="600" style="width: 600px">
%%=v(@JSON)=%%
</td>
</tr>
</table>

--

--

--

I am a certified Salesforce Marketing Cloud Consultant at Bower House Digital in Melbourne, Australia.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Laravel: Register your Manager in the Service Provider

Playing ESP32 is Easy and Fun![Bluetooth Classic]

1) Sign up for the Solana Hackathon ☀️ 2) Build on Orca 🐳 3) Profit! 💸

Expecting Dishonesty

Running a Node Server on the Pi

The most popular DBMS (database management systems) in the world in 2020

First 5 things to start learning PyTorch Tensors in Sagemaker Notebooks

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Charlie Fay

Charlie Fay

I am a certified Salesforce Marketing Cloud Consultant at Bower House Digital in Melbourne, Australia.

More from Medium

Salesforce Launched Net Zero Cloud 2.0:

Create Custom Campaign Statuses from a Template Automagically

Heroku postgres Integration with Force.com

SFDX+VSCode : Scratch Org Automation with VSCode Task