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>
Solved! Go to Solution.
There's no built-in collection filtering w/predicates in Velocity. So you do a simple loop and move the matches into another collection:
#set( $domainProjects = [] )
#foreach( $project in $allProjects )
#if( $project.domain.equalsIgnoreCase($lead.emailDomain) )
#set( $void = $domainProjects.add($project) )
#end
#end
#foreach( $project in $domainProjects )
## this is your subset of matches
#end
Your {{lead.email domain}} token, when you check it off and drag it onto the Velocity canvas, isn't going to have a space in it (the Velocity $lead.property name isn't the same as the token name). So I've used $lead.emailDomain here.