Expand my Community achievements bar.

Join our product experts for a live Ask Me Anything on November 12th at 8 AM PT about Experiences & Efficiency with AEP Agent Orchestrator & How Agentic AI is Fueling Smarter Testing and Growth!
SOLVED

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 Accepted Solution

Avatar

Correct answer by
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

View solution in original post

2 Replies

Avatar

Correct answer by
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

Avatar

Administrator

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