How to display beautified JSON in an field

Devishree Anant
Tera Contributor

Hi all,

 

I am getting the below response from a GET method:

 

DevishreeAnant_0-1665054420413.png

 

It needs to be updated in a string field (custom field created for this). But it should be updated with correct formatting as below:

 

DevishreeAnant_1-1665054494479.png

 

Can anyone suggest me a solution for this?

 

TIA

 

6 REPLIES 6

Murthy Ch
Giga Sage

Hi @Devishree Anant 

You just need to set the value like below:

Assume arr contains the data:

 

JSON.stringify(arr,null,4);

 

(=tested)

 

 

 

Thanks,
Murthy

I tried the same, but it isn't working

 

Getting the response like this only:

 

DevishreeAnant_0-1665055112293.png

 

Murthy Ch
Giga Sage

Hi @Devishree Anant 

Can you share the complete code.

Don't use stringify directly set the response with null,4

 

Thanks,
Murthy

@Murthy Ch , please find the complete code:

 

CODE:

 

// 5 minutes ago, used to check for new notes since the previous run
// NOTE: This will need updated if the scheduled job interval changes
var curDateTime = new GlideDateTime();
curDateTime.addSeconds(-300);

// Get all tickets updated since our last run (notes count as updates and will set the updated timestamp on tickets)
var r = new sn_ws.RESTMessageV2('DT_FS_V2', 'Get All Service Request');

var endpoint = r.getEndpoint();
endpoint += '&updated_since=' + curDateTime.getValue().split(' ').join('T') + 'Z';
r.setEndpoint(endpoint);
gs.log('DT_FS_updated_endpoint = ' + r.getEndpoint());

var response = r.execute();

gs.log('DT_FS_Response = ' + response);
var responseBody = response.getBody();
gs.log('DT_FS_Response_body = ' + responseBody);

var httpStatus = response.getStatusCode();
gs.log('DT_FS_HTTP_status_code = ' + httpStatus);

// If OK status, process response
if (httpStatus == 200) {
var json = new JSON();
gs.log('DT_FS_new_json_obj = ' + json);

var obj = json.decode(responseBody);
gs.log('DT_FS_decoded_resp_body_obj = ' + obj);

var incident_list = obj.tickets;
gs.log('DT_FS_incident_list = ' + JSON.stringify(incident_list));

var default_company = gs.getProperty('Fresh service default company');
gs.log('DT_FS_Default_cmpny_sys_prop = ' + default_company);


// Process each ticket
for (var i = 0; i < incident_list.length; i++) {
var specificTicket = new sn_ws.RESTMessageV2('DT_FS_V2', 'Get Specific SER REQ');
specificTicket.setStringParameter('ticket_id', incident_list[i].id);

var specificResponse = specificTicket.execute();
var specificTicketBody = specificResponse.getBody();
gs.log('DT_FS_spec_ticket_body = ' + specificTicketBody);

var req_items = specificTicketBody;
gs.log('REQ_ITEM_NEW_1 = ' + req_items);

var str_new_2 = JSON.stringify(req_items, null, 4);

gs.log('REQ_ITEM_10 = ' + str_new_2);

var specificTicketStatus = specificResponse.getStatusCode();
gs.log('DT_FS_spec_ticket_status = ' + specificTicketStatus);

gs.log('Ticket_ID = ' + incident_list[i].id.toString());


var inc = new GlideRecord('incident');
inc.addQuery('u_fs_id', 'SR-' + incident_list[i].id.toString());
inc.query();

if (inc.next()) {

{
inc.short_description = incident_list[i].subject.toString();
gs.log('DT_FS_ShortDesc_Update = ' + inc.short_description);

inc.u_fs_sr_variables = str_new_2;
gs.log('INC_FS_SR_Var_Update = ' + inc.u_fs_sr_variables);


}

inc.update();

} else {


inc.initialize();

inc.u_fs_id = 'SR-' + incident_list[i].id.toString();

inc.short_description = incident_list[i].subject.toString();
gs.log('DT_FS_ShortDesc_Insert = ' + inc.short_description);

inc.u_fs_sr_variables = str_new_2;
gs.log('INC_FS_SR_Var_Insert = ' + inc.u_fs_sr_variables);

 

inc.insert();

}

 

}

}

 

OUTPUT:

 

DevishreeAnant_0-1665137931013.png