Expand my Community achievements bar.

Join Adobe Journey Optimizer product experts for a live Ask Me Anything on October 15th at 8 AM PT about Expanding Your Mobile Reach with RCS & WhatsApp in AJO!

how to define and populate the values for an object at each iterated individual array

Avatar

Level 2

I'm working on AJO API Triggered Campaigns, well I'm trying to write a logic populate the personalization values from API JSON Payload on a HTML Template using below logic and json paylaod!

Question: help me how to define and populate the value for "name"  form below json payload at each iterated individual array level for an example

 

if countProducts = 1 then i want to print the value of attribute (name) at array level[1] with in the html template in this case the level[1] name value = "Super Socks"


AJO logic:

{% let countProducts = count(context.products) %}
{{#each context.products}}
{%#if countProducts = "0"or countProducts = "null"%}
 <p>You have no products to display.</p>
{%else%}
  <li>Product: {{this.name}}</li>
  {%/if%}
{{/each}}

{%#if (toString(countProducts) = "3" )%}
 <li>Product: {{context.products.name}}</li>
{%else%}
  <p>You have no products to display.</p>
{%/if%}

Results:


JSON:

{
  "context": {
    "products":  [
        {
          "name": "Super Socks",
          "price": 10.99,
          "sku": "SOCK123"
        },
        {
          "name": "Cool Hat",
          "price": 19.99,
          "sku": "HAT456"
        },
        {
          "name": "Cozy Scarf",
          "price": 15.49,
          "sku": "SCARF789"
        }
      ]
    }
  }

Results:(populated using https://experienceleague.adobe.com/en/apps/journey-optimizer/ajo-personalization?lang=en )

  • Product: Super Socks
   
  • Product: Cool Hat
   
  • Product: Cozy Scarf
   
  • Product:
countProducts : 3
Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Reply

Avatar

Community Advisor

@Nutsa9858 

  • Try this,
{% let countProducts = count(context.products) %}
{{#each context.products}}
{%#if countProducts = "0"or countProducts = "null"%}
<p>You have no products to display.</p>
{%else%}
<li>Product: {{this.name}}</li>
{%/if%}
{{/each}}

{%#if (toString(countProducts) = "3" )%}
<li>Product: {%= topN(context.products.name,context.products.name,1) %}</li>
{%else%}
<p>You have no products to display.</p>
{%/if%}
 
Output:
  • Product: Cool Hat
  • Product: Cozy Scarf
  • Product: Cozy Scarf123
  • Display Product: ["Cool Hat"]
  • If the countproducts=1, you could use the head function like below.
{% let countProducts = count(context.products) %}
{{#each context.products}}
{%#if countProducts = "0"or countProducts = "null"%}
<p>You have no products to display.</p>
{%else%}
<li>Product: {{this.name}}</li>
{%/if%}
{{/each}}

{%#if (toString(countProducts) = "1" )%}
<li>Display Product: {%= head(context.products.name) %}</li>
{%else%}
<p>You have no products to display.</p>
{%/if%}
 
Output:
  • Product: Cool Hat
  • Product: Cozy Scarf
  • Product: Cozy Scarf123
  • Display Product: Cool Hat
 
 
 
Thanks, Sathees