how to define and populate the values for an object at each iterated individual array | Community
Skip to main content
New Participant
October 7, 2025
Solved

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

  • October 7, 2025
  • 2 replies
  • 284 views

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
Best answer by SatheeskannaK

@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
 
 
 

2 replies

Sukrity_Wadhwa
Employee
October 17, 2025

Hi @nutsa9858,

Was the given solution helpful to resolve your query or do you still need more help here? Do let us know. In case the given solution was helpful, then kindly choose it as the 'Correct Reply'.

Thanks!

Sukrity Wadhwa
SatheeskannaK
SatheeskannaKAccepted solution
New Participant
October 7, 2025

@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