How to write if statement in Jelly Code for Object

Saif7
Tera Contributor

Hello,

 

I have the following code: <j2:forEach items="$[jvar_grCategories]" var="jvar_json"> , this should return N object so i need to write an if statement if( jvar_json.type == "car" ) then print "Type:Car".

 

I wrote the following code but the code is not working?

 

<j2:if test="$[ jvar_json.type == 'car']">

Type : $[jvar_json.type]

</j2:if>

 

Thanks

2 REPLIES 2

SuhasPSalunkhe
Kilo Guru

jaheerhattiwale
Mega Sage
Mega Sage

@Saif7 Tried and tested solution.

 

I have added the full code I tried below.

 

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<g:evaluate var="jvar_grCategories">
var categories = [{
"type":"car"
},{
"type":"truck"
}];
categories;
</g:evaluate>


<j:forEach var="jvar_json" items="${categories}">
<j:if test="${jvar_json.type == 'car'}">
Type : ${jvar_json.type}
</j:if>
</j:forEach>

</j:jelly>

 

Result:

jaheerhattiwale_0-1671100398064.png

 

Note:

When I used jvar_grCategories it was not working but when I used categories it worked. So please try like this. If it does not work out then please post full code.

 

Please mark as correct answer if this solves your issue.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023