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

Mohith Devatte
Tera Sage
Tera Sage

Hello @geet ,

can you give the JSON that is printed in this log?

gs.log("checking the JSON format" +jsonObj);

Hi Mohith,

It is giving below log:

@geet  can you try this log  and send it to me again

gs.log("checking the JSON format" +JSON.stringify(jsonObj)); 

 

it returned the below:

 

checking the JSON format{"event_sys_id":"00bb76c7db3bcd941f85287f05961924","bucket":{},"delayTimeMin":"1432","metric_value":"1432","critical_threshhold":"120"}