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

I am testing with some scenarios, Please allow me sometime.

I will surely mark your answer as correct once I complete testing.

@geet  sure let me know in case of issues

Hi Mohith,

 

I have tested it and it is working fine.

I would like to mark the answer correct but before that I would like to request you to please share the approach you decided to give a fix for this issue?

I have worked with JSON but when I got stuck with this issue, I searched other community articles so that I can figure out myself but I couldn't and then landed here for asking for help.

Could you please guide here so that next time I do it myself rather than posting.

So the way that i did it if you see or accessing an empty key value par it says "undefined"

so i just checked  in loop whether it is empty or not by comparing it with undefined keyword .

So only store or append the value if its not undefined and that made the trick