Parse json data into table

Madhavi2
Tera Contributor

Hi all,

 

I am getting below response from a script as json array. I would like to print that in dynamic table.

 

{"categoryKey":"Business Intelligence Sim|Enrollment Sim|Other","recentIncidents":[{"number":"INC4449511"},{"number":"INC4449512"}],"historicIncidents":[{"number":"INC4306467"},{"number":"INC4347468"},{"number":"INC4359174"},{"number":"INC4380294"},{"number":"INC4422274"},{"number":"INC4449511"},{"number":"INC4449512"}]}

 

I am trying with below script.

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    var category = payload.categoryKey;
    var recentIncidentNum = [];
    var recentIncidents = payload.recentIncidents;
    for (i = 0; i < recentIncidents.length; i++) {
        recentIncidentNum.push(recentIncidents[i].number.toString());
    }
    var historicIncidentNum = [];
    var historicIncident = payload.historicIncidents;
    for (j = 0; j < historicIncident.length; j++) {
        historicIncidentNum.push(historicIncident[j].number.toString());
    }
 	
	template.print('<table border="1px solid black">');

template.print( "<tr bgcolor='#ddd' align='center'>" );
template.print("<td style='text-align:center' colspan='3'><strong>Similar Incidents</strong></td>");
template.print( "</tr>" );

template.print( "<tr>" );
template.print( "<td><left><b>" + "Recent Incidents" + "</b></left></td>" );
template.print( "<td><left><b> "+"Historic Incidents"+" </b></left></td>" );
template.print( "<td><left><b> "+"Category"+" </b></left></td>" );
template.print( "</tr>" );

template.print( "<tr>" );
template.print( "<td><left>" + recentIncidentNum + "</left></td>" );
template.print( "<td><left>" +historicIncidentNum+ "</b></left></td>" );
template.print( "<td><left>" +category +"</left></td>" );

template.print( "</tr>" );
template.print('</table>');

})(current, template, email, email_action, event);

 

It is getting printed as below.

 

Madhavi2_0-1709909946093.png

 

Would like to print Recent Incidents and historic incidents line by line dynamically based on number of incidents.

Please assist me on this.

2 REPLIES 2

AshishKM
Kilo Patron
Kilo Patron

Hi @Madhavi2 , 

You can apply the for loop with template.print using the line break html tag "<b>".

 

 

-Thanks,

AshishKM


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Sumanth16
Kilo Patron

Hi @Madhavi2 ,

 

To create a record in a service table from a JSON response, you will need to follow these general steps:

1. Parse the JSON response: Use a JSON parser to convert the JSON response
2. Extract the relevant data
3. Connect to the service table
4. Insert the data

------------------------------------------------------------------
var MyScriptInclude = Class.create();
MyScriptInclude.prototype = {
initialize: function() {},

createRecordFromJSON: function(jsonResponse) {
var responseObj = JSON.parse(jsonResponse);

var name = responseObj.name;
var email = responseObj.email;
var age = responseObj.age;

var gr = new GlideRecord('your table');
gr.initialize();

gr.name = name;
gr.email = email;
gr.age = age;

gr.insert();
}
};

 

// Usage example
var jsonResponse = '{"name": "John Doe", "email": "john.doe@example.com", "age": 30}';
var myScriptInclude = new MyScriptInclude();
myScriptInclude.createRecordFromJSON(jsonResponse);

 

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,

Sumanth Meda