Last active
April 25, 2023 20:48
-
-
Save wvpv/aa9580f976c85c425761385d088d024f to your computer and use it in GitHub Desktop.
Parse ASC JSON payload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<style> | |
body, a, input {font-family:sans-serif;} | |
</style> | |
</head> | |
<body style="font-family:sans-serif"> | |
%%=now()=%% | |
<h1>JSON Parse with GTL</h1> | |
%%[ | |
/* Sample JSON payload: https://gist.github.com/wvpv/4550507df4714f9f81fb6da843ba4162 */ | |
var @json | |
set @json = AttributeValue("json") | |
if not empty(@json) then | |
]%% | |
{{.dataobject JsonVar type=variable source=@json maxrows=20}} | |
{{.data}} | |
{"target":"@json"} | |
{{/data}} | |
{{/dataobject}} | |
{{#each JsonVar}} | |
<br><a href="%%=RedirectTo(TreatAsContent('{{url}}'))=%%">{{sku}}</a> - <a href="%%=RedirectTo(TreatAsContent('{{url}}'))=%%">{{name}}</a> | |
{{/each}} | |
%%[ | |
else | |
outputline(concat("<br>no JSON found")) | |
endif | |
]%% | |
<h1>JSON Parse with SSJS</h1> | |
<script runat="server"> | |
Platform.Load("Core","1"); | |
var json = Attribute.GetValue('json'); | |
var jsonObj = Platform.Function.ParseJSON(json); | |
if (jsonObj.length > 0) { | |
for (var i = 0; i < jsonObj.length; i++ ) { | |
var item = jsonObj[i]; | |
Platform.Variable.SetValue("@sku",item['sku']); | |
Platform.Variable.SetValue("@name",item['name']); | |
Platform.Variable.SetValue("@url",item['url']); | |
</script> | |
<br><a href="%%=redirectto(@url)=%%">%%=v(@sku)=%%</a> - <a href="%%=redirectto(@url)=%%">%%=v(@name)=%%</a> | |
<script runat="server"> | |
} | |
} else { | |
Write("no products found") | |
} | |
</script> | |
<br><br><br><a href="%%profile_center_url%%">Profile/Preference Center</a> | |
<br><a href="%%subscription_center_url%%">Subscription Center</a> | |
<br><a href="%%unsub_center_url%%">One-Click Unsubscribe</a> | |
<p>This email was sent by: | |
<b>%%Member_Busname%%</b> | |
<br>%%Member_Addr%%, %%Member_City%%, %%Member_State%% %%Member_PostalCode%% %%Member_Country%%</p> | |
<custom name="opencounter" type="tracking"> | |
</body> | |
</html> |
You'll need to escape the JSON data in your API call as a string:
POST /messaging/v1/messageDefinitionSends/key:YOURTSDKEYHERE/send HTTP/1.1
Host: https://YOURTENANTHERE.rest.marketingcloudapis.com/
Authorization: Bearer YOURTOKENHERE
Content-Type: application/json
{
"To": {
"Address": "test@example.com",
"SubscriberKey": "test@example.com",
"ContactAttributes": {
"SubscriberAttributes": {
"FirstName": "John",
"JSON": "[{\"sku\": \"123\",\"name\": \"Square\",\"url\": \"https://limedash.com?sku=123\"}, {\"sku\": \"456\",\"name\": \"Circle\",\"url\": \"https://limedash.com/?sku=456\"}, {\"sku\": \"789\",\"name\": \"Triangle\",\"url\": \"https://limedash.com/?sku=789\"}]"
, "FieldNotRecorded" : "whee!"
}
}
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I could use some help with this please! I used the ssjs way in the past and it worked like a charm. For a new email, the client is now nesting data. I am struggling to get the nesting to work. I would really appreciate any help getting the nested data to populate.
an example data piece looks like this:
{
"offerIdMap": {
"O-24660":{
"qty":"2","displayName":"Basic kit","price":"$99","type":"PRODUCTBASIC"
}
},
"coupons":[]
}
Thanks for any help and insights you could provide!