0

I need to create an automated email that will be triggered and sent to customer whenever a customer has requested to cancel a number of items within their order. I'm using both ampscript variables and json data for items and this is nested in data extension. As this is automated, I won't know how long the list of items could be.

  1. How can I set the json automatically know the list of items every time email is triggered. In examples I've seen people know the json data from the beginning.
  2. My code is also not working even when I try to test once telling what the json data should be.

This is my code so far but when I activate in journey builder - it will not activate - throws error.

    %%[
    VAR @json
    SET @json = '{
            "items": [{
                    "title": "Tea Bags",
                    "sku": "QQ-111111",
                    "qty": 1
                    },
                    {
                    "title": "Biscuits",
                    "sku": "QQ-222222",
                    "qty": 10
                    }
            ]
        }'
    ]%%
    {{.datasource JSONVar type=variable maxRows = 5000}}
    {{.data}}
              {"target" : "@json"}
    {{/data}}
            {{.datasource items type=nested maxRows = 5000}}
            {{.data}}
                { "target" : "items" }
            {{/data}
        <td width="75%" align="left">
                            <a href="https://www.website.co.uk/{{sku}}%" title="{{title}}">{{title}}</a>
                            <br/><span style="font-size:11px; color:#999999;">Order Code (SKU): {{sku}}</span>
                        </td>
                        <td width="25%" align="center">{{qty}}%</td>
            {{/datasource}}
    {{/datasource}}
                    </tr>
                </table>
            </div>
        </td>
    </tr>

I've updated my code to this and the values will not render in the email but are stored in the field in the data extension:

New code:

      <tr>
    %%[
    VAR @jsonItems
    Set @jsonItems = [items]
    ]%%
    {{.datasource jsonItems type=variable maxRows = 5000}}
    {{.data}}
       {"target": "@jsonItems"}
    {{/data}}
       {{.datasource items type=nested}}
       {{.data}}
          {"target": "jsonItems.items"}
       {{/data}}
          <td width="400px" align="left">
        <a href="https://www.website.co.uk/{{items.sku}}"  title="{{items.title}}">{{items.title}}</a>
        <br/><span style="font-size:11px; color:#999999;">Order Code (SKU): {{items.sku}}</span>
       </td>
          <td align="center" width="80px">{{items.qty}}</td>
       {{/datasource}}
    {{/datasource}}
      </tr>

This is how items is stored in the data extension enter image description here

Screenshot rendering in 1 row: enter image description here

Updated Code with each function:

%%[
VAR @jsonItems
Set @jsonItems = [items]
]%%

{{.datasource jsonItems type=variable maxRows = 50}}
{{.data}} 
     {"target": "@jsonItems"}
{{/data}} 
     {{#each jsonItems}}
      <td width="400px" align="left">
    <a href="https://www.website.co.uk/{{sku}}"  title="{{title}}">{{title}}</a>
    <br/><span style="font-size:11px; color:#999999;">Order Code (SKU): {{sku}}</span>
   </td>
      <td align="center" width="80px">{{qty}}</td>
     {{/each}}
{{/datasource}}

1 Answer 1

1

The below worked for me:

%%[
VAR @jsonItems
Set @jsonItems = '{"items":[{"title":"Tea Bags","sku":"QQ-111111","qty":1},{"title":"Biscuits","sku":"QQ-222222","qty":10}]}'
]%%
{{.datasource jsonItems type=variable maxRows = 1}}
{{.data}}
   {"target": "@jsonItems"}
{{/data}}
   {{.datasource items type=nested}}
   {{.data}}
      {"target": "jsonItems.items"}
   {{/data}}
       {{items.title}} | {{items.sku}} | {{items.qty}} <br>
   {{/datasource}}
{{/datasource}}
  • There was an extra % sign in your code;
  • Nested data source needed "parent" source (changed items to jsonItems);

Output looked like this:

Rendered Output

(Removed HTML and renamed variables to make it easier to debug and paste into StackExchange. It wasn't source of problem.)

--- Updated code to address JSON update

%%[
VAR @jsonItems
Set @jsonItems = [items]
]%%

{{.datasource jsonItems type=variable maxRows = 50}}
{{.data}}
   {"target": "@jsonItems"}
{{/data}}
   {{title}} | {{sku}} | {{qty}} <br>
{{/datasource}}
5
  • Thank you. I've tried that but now the email doesn't render the content. I've updated my Q to include the amends and also added a screenshot of how the items is stored in the data extension.
    – T01
    Commented Mar 31, 2023 at 14:32
  • @T01 - I've added the GTL code to address changes to the JSON and the source coming from the DE column. When testing with the JSON you shared in the DE, I'm able to print the nodes w/in the datasource blocks. Removed HTML for easier testing. Please mark answered if this helped. Commented Mar 31, 2023 at 19:01
  • Thank you so much for your help! Very appreciated. That works for me now. However, do I need an {{#each}} {{/each}} section for items? As the items in the email template rendering in 1 row horizontally but I require the list to go vertically? I've updated my Q with screenshot of what the email is doing now as well as the #each function I've added to your updated code.
    – T01
    Commented Apr 2, 2023 at 13:47
  • You won't need each in this case. It's your HTML, it currently goes <td></td><td></td> w/in the code block. It instead needs to be <tr><td></td><td></td></tr>. Commented Apr 2, 2023 at 18:08
  • Perfect, thank you for all your help :) It works for me as I wanted.
    – T01
    Commented Apr 3, 2023 at 7:21

You must log in to answer this question.

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