Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Need Help with Object Object

geet
Tera Guru

I am trying to put JSON format data into a table with the help of below script.

The script is returning object object for one of the value.

 

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var jsonObj =JSON.parse(current.additional_info);
	
	gs.log("checking the JSON format" +jsonObj);
	
	
	var style = '<style type="text/css">';
	style += '.tg  {border-collapse:collapse;border-spacing:0;}';
	style += '.tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;';
	style += '  overflow:hidden;padding:10px 5px;word-break:normal;}';
	style += '.tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;';
	style += '  font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;}';
	style += '.tg .tg-akbm{font-weight:bold;text-align:left;text-decoration:underline;vertical-align:top}';
	style += '.tg .tg-0lax{text-align:left;vertical-align:top}';
	style += '.tg .tg-7zrl{text-align:left;vertical-align:bottom}';
	style += '</style>';

	var tableHead = '<table class="tg">';
	tableHead += '<thead>';
	tableHead += '  <tr>';
	tableHead += '    <th class="tg-akbm">Alert Field</th>';
	tableHead += '    <th class="tg-akbm">Alert Value</th>';
	tableHead += '  </tr>';
	tableHead += '</thead>';
	tableHead += '<tbody>';

	var tableBody = '';
	for (key in jsonObj){
		if(jsonObj[key] != ''){
			tableBody += '  <tr>';
			tableBody += '    <td class="tg-0lax">' + key + '</td>';
			tableBody += '    <td class="tg-7zrl">' + jsonObj[key] + '</td>';
			tableBody += '  </tr>';
		}
	}

	var tableEnd = '</tbody>';
	tableEnd += '</table>';

	var destinationValue = style + tableHead + tableBody + tableEnd;
	current.u_additional_info = destinationValue;

})(current, previous);

This is the format of JSON below:

{"event_sys_id":"00bb76c7db3bcd941f85287f05961924","bucket":{},"delayTimeMin":"1432","metric_value":"1432","critical_threshhold":"120"}

After running the script I am getting below output:

 

Alert FieldAlert Value
event_sys_id00bb76c7db3bcd941f85287f05961924
bucket[object Object]
delayTimeMin1432
metric_value1432
critical_threshhold120

I need help with this object object.

The bucket field should have empty value.

Please suggest how can this be fixed.

1 ACCEPTED SOLUTION

@geet  please follow below script

Object.keys(jsonObj).forEach(function(key) {
 
if(jsonObj[key].length!=undefined)
{
tableBody += '  <tr>';
			tableBody += '    <td class="tg-0lax">' + key + '</td>';
			tableBody += '    <td class="tg-7zrl">' + jsonObj[key]+ '</td>';
			tableBody += '  </tr>';
}
else
{
tableBody += '  <tr>';
			tableBody += '    <td class="tg-0lax">' + key + '</td>';
			tableBody += '    <td class="tg-7zrl"></td>';
			tableBody += '  </tr>';
}

})

Please try this below script this worked for me 

if it works please mark my answer correct

View solution in original post

13 REPLIES 13

@geet  please try to replace the below snippet in your code 

for (key in jsonObj)

{ if(jsonObj[key] != '')

{

tableBody += ' <tr>'; tableBody += ' <td class="tg-0lax">' + key + '</td>'; tableBody += ' <td class="tg-7zrl">' + jsonObj[key] + '</td>'; tableBody += ' </tr>';

}

with this and try

Object.keys(jsonObj).forEach(function(key) {
 
tableBody += '  <tr>';
			tableBody += '    <td class="tg-0lax">' + key + '</td>';
			tableBody += '    <td class="tg-7zrl">' + jsonObj[key]+ '</td>';
			tableBody += '  </tr>';
})

@geet  modified my code a bit please try the above code and if it works please mark my answer correct

The result is still the same.

 

@geet  please follow below script

Object.keys(jsonObj).forEach(function(key) {
 
if(jsonObj[key].length!=undefined)
{
tableBody += '  <tr>';
			tableBody += '    <td class="tg-0lax">' + key + '</td>';
			tableBody += '    <td class="tg-7zrl">' + jsonObj[key]+ '</td>';
			tableBody += '  </tr>';
}
else
{
tableBody += '  <tr>';
			tableBody += '    <td class="tg-0lax">' + key + '</td>';
			tableBody += '    <td class="tg-7zrl"></td>';
			tableBody += '  </tr>';
}

})

Please try this below script this worked for me 

if it works please mark my answer correct

@geet  did it work?

please see the output 

Try the above script and let me know 

if it works please mark my answer correct and close the thread

find_real_file.png