Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Parse ASC JSON payload
body, a, input {font-family:sans-serif;}
<body style="font-family:sans-serif">
<h1>JSON Parse with GTL</h1>
/* Sample JSON payload: */
var @json
set @json = AttributeValue("json")
if not empty(@json) then
{{.dataobject JsonVar type=variable source=@json maxrows=20}}
{{#each JsonVar}}
<br><a href="%%=RedirectTo(TreatAsContent('{{url}}'))=%%">{{sku}}</a> - <a href="%%=RedirectTo(TreatAsContent('{{url}}'))=%%">{{name}}</a>
outputline(concat("<br>no JSON found"))
<h1>JSON Parse with SSJS</h1>
<script runat="server">
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];
<br><a href="%%=redirectto(@url)=%%">%%=v(@sku)=%%</a> - <a href="%%=redirectto(@url)=%%">%%=v(@name)=%%</a>
<script runat="server">
} else {
Write("no products found")
<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:
<br>%%Member_Addr%%, %%Member_City%%, %%Member_State%% %%Member_PostalCode%% %%Member_Country%%</p>
<custom name="opencounter" type="tracking">

This comment has been minimized.

Copy link

@kruntis kruntis commented Jul 8, 2020

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": {
"qty":"2","displayName":"Basic kit","price":"$99","type":"PRODUCTBASIC"

Thanks for any help and insights you could provide!


This comment has been minimized.

Copy link
Owner Author

@wvpv wvpv commented Jul 8, 2020

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
Authorization: Bearer YOURTOKENHERE
Content-Type: application/json

    "To": {
        "Address": "",
        "SubscriberKey": "",
        "ContactAttributes": {
            "SubscriberAttributes": {
                "FirstName": "John",
                "JSON": "[{\"sku\": \"123\",\"name\": \"Square\",\"url\": \"\"}, {\"sku\": \"456\",\"name\": \"Circle\",\"url\": \"\"}, {\"sku\": \"789\",\"name\": \"Triangle\",\"url\": \"\"}]"
                , "FieldNotRecorded" : "whee!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.