2

I am building an email using data passed through JSON. Here is an example of the JSON file:

{
  "order": {
    "items": [
      {
        *Other Data*,
        "tracking": [
          {
          **"number": "8768123231",
          "url": "website.com"**
          },
          {
          **"number": "1234324234",
          "url": "website.com"**
          }
        ]
      },
      {
        *Other Data*,
        "tracking": [
          {
          **"number": "987123123",
          "url": "website.com"**
          }
        ]
      }
    ]
  }
}

The data I need to access if the tracking numbers. I was able to loop through the different items in the JSON by using SSJS and GTL like this:

<script runat=server>
    function set(key, val) {
      var newVal = val != null ? Stringify(val) : "{}";
      Variable.SetValue(key, newVal);
    }
    Platform.Load("Core","1");
    var Json = Variable.GetValue("@Json");
    var _JsonObject = Platform.Function.ParseJSON(Json);
    set("@items", _JsonObject.order.items);
</script>

+

{{.dataobject items type=variable}}
    {{.data}}
        {"target":"@items"}
    {{/data}}
{{/dataobject}}
{{#each items}}
    *Print Out Item Details*
{{/each}}

Given the unknown number of items and tracking numbers associated with those items, I think the only way to make it work is to have an each loop for the tracking numbers within the items each loop.

Something like this:

{{.dataobject items type=variable}}
    {{.data}}
        {"target":"@items"}
    {{/data}}
{{/dataobject}}
{{#each items}}
    *Print Out Item Details*
    {{.dataobject tracking type=variable}}
        {{.data}}
            {"target":".this.tracking"}
        {{/data}}
    {{/dataobject}}
    {{#each tracking}}
        *Print Out Tracking Number*
    {{/each}}
{{/each}}

But obviously, I can't figure out how to get something like that to work. I'm not sure if what I'm trying to do is even possible so any guidance or advice would be appreciated.

1 Answer 1

1

I managed to find a solution here under Nested Datasource Example: https://developer.salesforce.com/docs/atlas.en-us.noversion.mc-programmatic-content.meta/mc-programmatic-content/gtlDatasourceTagExamples.htm

I literally copy/paste the example and got a working version. You can nest the GTL to loop again through your tracking numbers.

Hope this helps!

1
  • I was able to solve the issue thanks to that example. The issue I was having was you can only nest down 1 level at a time while when I tried this solution earlier, I was trying to go down 3 levels in one go. I have been able to refactor my code to no longer require SSJS or the {{#each}} statement. Thanks!
    – Alex Cobb
    Commented Jan 17, 2019 at 13:55

You must log in to answer this question.

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