AnsweredAssumed Answered

Conditional forEach statement in Velocity Script

Question asked by Jon Wright on May 1, 2019
Latest reply on May 3, 2019 by Jon Wright

I've been using Sanford Whiteman's excellent resource on using JSON in Velocity script but am stuck on how I'd go about only displaying certain records from the JSON.

 

For example in the following I only want to list the relevant values where  {{lead.email domain}} = allProjects.domain. So anyone with a domain of company01.com would see the first 2 records listed, whereas company02.com would only see the last record (there will be about 50 - 100 records in the JSON eventually). Any pointers greatly appreciated!

 

#set( $allProjects = [
{
    "domain": "Company01.com",
"category": {
        "URL": "https://www.example.com/project1",
        "companyName": "Company 01",
        "name": "Example Project Name 01",
"participantType": "Champion"
    }
},
{
    "domain": "Company01.com",
"category": {
        "URL": "https://www.example.com/project2",
        "companyName": "Company 01",
        "name": "Example Project Name 02",
"participantType": "Champion"
    }
},
{
    "domain": "Company02.com",
"category": {
        "URL": "https://www.example.com/project1",
        "companyName": "Company 02",
        "name": "Example Project Name 01",
"participantType": "Champion"
    }
}
}
] )

<p><br /></p>
<ul>
#foreach( ${allProjects.domain} in $allProjects )
<li>
<a href="${allProjects.category.URL}" target="_blank" id="">${allProjects.category.name}</a></li>
#end
</ul>

Outcomes