- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2022 02:12 PM
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 Field | Alert Value |
---|---|
event_sys_id | 00bb76c7db3bcd941f85287f05961924 |
bucket | [object Object] |
delayTimeMin | 1432 |
metric_value | 1432 |
critical_threshhold | 120 |
I need help with this object object.
The bucket field should have empty value.
Please suggest how can this be fixed.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2022 03:58 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2022 02:30 PM
Hello
can you give the JSON that is printed in this log?
gs.log("checking the JSON format" +jsonObj);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2022 02:36 PM
Hi Mohith,
It is giving below log:
checking the JSON format[object Object] |
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2022 02:41 PM
gs.log("checking the JSON format" +JSON.stringify(jsonObj));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2022 02:52 PM
it returned the below:
checking the JSON format{"event_sys_id":"00bb76c7db3bcd941f85287f05961924","bucket":{},"delayTimeMin":"1432","metric_value":"1432","critical_threshhold":"120"}