0

This is my JSON request :

{
  "definitionKey": "SSJSTEST19",
  "recipient":
  {
   "contactKey": "007",
    "to": "sourabxxxxx",
    "attributes": {
     "EmailAddress": "sourabxxxxx",
    "SubscriberKey": "Sourab",
    "CUSTOMERNR": "123678",
    "FirstName": "Sourab",
    "Packages": [
         { "Name" : "Entertainment",
           "Price" : "15 €",
           "PaymentDate" : "01.07.2020"
},
{ "Name" : "Cinema",
           "Price" : "15 €",
           "PaymentDate" : "01.07.2020"
}
]     
      }

  }
}

Here is my Code :

<table cellpadding="0" cellspacing="0" width="100%">

  <tr>
   <td align="center">
    <table cellpadding="0" cellspacing="0" class="container" width="550">

      <tr>
       <td align="left" style="font-family: Helvetica,Arial,sans-serif; font-size: 16px; color: #4a4a4a; padding:px; line-height:24px; background:#F9F9F9;">
%%[
var @MyArray,@Array,@AllPackages,@email,@contactkey ,@First_Name,@CustomerNumber 
set @email= emailaddr 
set @contactkey = AttributeValue("Contact_Key") 
set @CustomerNumber = AttributeValue("CUSTOMERNR") 
set @First_Name = AttributeValue("FirstName") 
set @AllPackages = AttributeValue("Packages") 
]%%

<div style="text-align: right;">Kundennummer: %%=V(@CustomerNumber)=%%</div>
<br>
<b>Hallo %%=V(@First_Name)=%%</b>,<br>
<script runat="server">
Platform.Load("Core","1");
 var arr = [];
 var jsonString1 = Variable.GetValue("@AllPackages");
 arr.push(Platform.Function.ParseJSON(jsonString1).message.views[0].content);
 Write(Stringify(arr));
 Variable.SetValue("@Array",arr);
</script> 

%%[
SET @MyArray = @Array 
]%%
<br>
%%=v(@MyArray)=%%
<br>

       </td></tr></table></td></tr></table>

Result - After triggering the email, the array yields [null] result.

8
  • As a best practice, do not use SSJS in emails. An alternative specifically for your usecase of JSON parsing is GTL / Guide Template Language. Eliot Harper has a good example here: github.com/eliotharper/gtl-demo Jun 3, 2020 at 8:53
  • I used GTL, it works only when I pass the values explicitly in the code. I cannot fetch this array which comes in the form of nested data in JSON. Jun 3, 2020 at 12:56
  • Hi, so what you're saying is, you can get to the "recipient" object, but not the "packages" array with GTL? Jun 3, 2020 at 13:10
  • No.. I meant I cannot access "Packages" array using GTL. Jun 3, 2020 at 13:46
  • I think we mean the same :) It should be possible. Check this thread: salesforce.stackexchange.com/questions/246729/… Jun 3, 2020 at 14:06

1 Answer 1

1

The below should work for your needs to output the stringified packages:

<script runat=server>
Platform.Load("Core","1");

 //set your attributes to variables
 var email = Attribute.GetValue('emailaddr');
 var contactkey = Attribute.GetValue('Contact_Key');
 var customerNumber = Attribute.GetValue('CUSTOMERNR');
 var first_Name = Attribute.GetValue('FirstName');
 var allPackages = Platform.Function.ParseJSON(Attribute.GetValue("Packages")).recipient.attributes.Packages;
 var packagesStr = Stringify(allPackages);

</script>

<table cellpadding="0" cellspacing="0" width="100%">

  <tr>
   <td align="center">
    <table cellpadding="0" cellspacing="0" class="container" width="550">

      <tr>
       <td align="left" style="font-family: Helvetica,Arial,sans-serif; font-size: 16px; color: #4a4a4a; padding:px; line-height:24px; background:#F9F9F9;">
            <br>
            <ctrl:var name=packagesStr />
            <br>
       </td>
      </tr>
     </table>
    </td>
   </tr>
</table>

Which outputs:

[{"Name":"Entertainment","Price":"15 €","PaymentDate":"01.07.2020"}, {"Name":"Cinema","Price":"15 €","PaymentDate":"01.07.2020"}]

You can then iterate through the JSON as you would normally:

var Name = allPackages[0].Name        //outputs Entertainment
var Price = allPackages[0].Price      //outputs 15 €
var obj2_Name = allPackages[1].Name   //outputs Cinema
var obj2_Price = allPackages[1].Price //outputs 15 €

When outputting SSJS variables inline, you need to use the ctrl tag. Like so:

<ctrl:var name=Name />   //outputs like AMPscript %%=v(@Name)=%%
<ctrl:var name=Price />  //outputs like AMPscript %%=v(@Price)=%%

When I ran the above, it ran in less than a second, so I do not foresee processing being an issue.

8
  • Thanks for the response. I tried with a simple approach by exactly pasting the above code to see if it renders anything but unfortunately I could still see the output as null. What could be the reason ? Jun 9, 2020 at 6:50
  • Can you share your code because it output for me as shown in my answer. I get the feeling that there is a syntax or parsing issue going on Jun 9, 2020 at 13:27
  • Code is exactly the same as yours (mentioned above by you). But in output I can only see the FirstName,EMail Address and other fields which have one value, but for Packages its Null Jun 11, 2020 at 9:43
  • Verify your data structure matches what you provided - e,g. that the path .recipient.attributes.Packages is correct inside your Packages attribute pulled in. If it is different, then you will need to adjust this path to what is in your data. Jun 11, 2020 at 14:41
  • To simplify and verify that the Packages attribute is coming across correctly, you can change the var allPackages to Attribute.GetValue("Packages") and the ctrl var name to allPackages Jun 11, 2020 at 14:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .