Jelly forEach using JSON object

BigMarkP
Giga Contributor

Hello -

I have a glide query which is with the returned results is assigning a value to my task obj and caller obj.

This object is converted into a json string object.  The output looks like this

[{"inc":"a9a16740c61122760004fe9095b7ddca","caller":"Joe Employee"},{"inc":"965c9e5347c12200e0ef563dbb9a7156","caller":"Bow Ruggeri"},{"inc":"ed92e8d173d023002728660c4cf6a7bc","caller":"David Miller"}]

Using my forEach loop I'd like to be able to grab the objects and build my id name and also output the results in my div.  

Oringally when I was not jusing a JSON object but just a basic array I was able to do something like this...

<j2:forEach items="$[allJSON]" var="jvar_bar">
<div id="incident-$[jvar_bar]-content" class="my-incident-content">
$[jvar_bar]
</div>
</j2:forEach>

But now as a JSON object with more data I would like to pull the inc sys id and caller and use that in my div id name and it's content.  Is there some way to access the JSON data using my forEach like where I could grab the task and caller value something like below?

<div id="incident-$[jvar_bar.task]-$[jvar_bar.caller]-content" class="my-incident-content"> 

<p> Results $[jvar_bar.task]-$[jvar_bar.caller] </p>

 

 

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">	  
	
	<html lang="en" class="ltr" dir="ltr">
	<head>
	</head>
	<body>

	<div>
		<div class="left-container" style="float:left;width:12%">			
			<g2:evaluate>
				var incs = [];					
				var gr = new GlideRecord('incident');
				gr.addQuery('active=true^short_descriptionLIKEemail');				
				gr.query();
				var myCount = gr.getRowCount();
				
				while (gr.next())
				
				{	
				var obj = {};
				obj.task = gr.getUniqueValue();
				obj.caller = gr.getDisplayValue("caller_id");
				incs.push(obj);
				}				
			
				var myJSON = JSON.stringify(incs);
				
			</g2:evaluate>		
			<div>
			
			<j2:forEach items="$[myJSON]" var="jvar_bar">	
				<div id="incident-$[jvar_bar.task]-$[jvar_bar.caller]-content" class="my-incident-content">				
					<p> Results	$[jvar_bar.task]-$[jvar_bar.caller]		</p>			
				</div>				
			</j2:forEach>
				
		</div>	
	</div> 
</div>
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Mark,

update code as below; your g2 tag should have object;

the below code should work;

see the changes in <g2:evaluate> tag and the last line inside g2:evaluate tag i.e. incs;

this tells to store the incs as object in the jvar_jsonObj and then use it 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">	  
	
	<html lang="en" class="ltr" dir="ltr">
	<head>
	</head>
	<body>

	<div>
		<div class="left-container" style="float:left;width:12%">			
			<g2:evaluate jelly="true" object="true" var="jvar_jsonObj">
				var incs = [];					
				var gr = new GlideRecord('incident');
				gr.addQuery('active=true^short_descriptionLIKEemail');				
				gr.query();
				var myCount = gr.getRowCount();
				
				while (gr.next())
				
				{	
				var obj = {};
				obj.task = gr.getUniqueValue();
				obj.caller = gr.getDisplayValue("caller_id");
				incs.push(obj);
				}				
			
				incs;
			</g2:evaluate>		
			<div>
			
			<j2:forEach items="$[jvar_jsonObj]" var="jvar_json">	
				<div id="incident-$[jvar_json.task]-$[jvar_json.caller]-content" class="my-incident-content">				
					<p> Results	$[jvar_json.task]-$[jvar_json.caller]		</p>			
				</div>				
			</j2:forEach>
				
		</div>	
	</div> 
</div>

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

2 REPLIES 2

ChrisBurks
Mega Sage

Everything is correct in your code except for one line:

var myJSON = JSON.stringify(incs);

With this line the JSON object is being turned into a string representation of the object. A string cannot be dot-walked. If the purpose is just to iterate through it then "stringification" is not needed.

var myJSON = incs;

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Mark,

update code as below; your g2 tag should have object;

the below code should work;

see the changes in <g2:evaluate> tag and the last line inside g2:evaluate tag i.e. incs;

this tells to store the incs as object in the jvar_jsonObj and then use it 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">	  
	
	<html lang="en" class="ltr" dir="ltr">
	<head>
	</head>
	<body>

	<div>
		<div class="left-container" style="float:left;width:12%">			
			<g2:evaluate jelly="true" object="true" var="jvar_jsonObj">
				var incs = [];					
				var gr = new GlideRecord('incident');
				gr.addQuery('active=true^short_descriptionLIKEemail');				
				gr.query();
				var myCount = gr.getRowCount();
				
				while (gr.next())
				
				{	
				var obj = {};
				obj.task = gr.getUniqueValue();
				obj.caller = gr.getDisplayValue("caller_id");
				incs.push(obj);
				}				
			
				incs;
			</g2:evaluate>		
			<div>
			
			<j2:forEach items="$[jvar_jsonObj]" var="jvar_json">	
				<div id="incident-$[jvar_json.task]-$[jvar_json.caller]-content" class="my-incident-content">				
					<p> Results	$[jvar_json.task]-$[jvar_json.caller]		</p>			
				</div>				
			</j2:forEach>
				
		</div>	
	</div> 
</div>

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader