Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

When I try to do it in Background Script, I could get the expected format in Log, but when I am trying to populate it with the string field, I am not able to do it:

 

Code in Background Script:

 

var initial_string = "{\"requested_items\":[{\"custom_fields\":{\"employee_name\":\"TEST Employee 3\",\"job_title\":\"Test Title 3\",\"manager\":19002284765,\"department\":\"CSO (101)\",\"employee_needs_an_access_badge\":false,\"new_badge_or_replacement\":\"New\",\"access_level_1_5\":\"2: Office/Lab\",\"employee_has_car\":false,\"license_plate_number_with_state\":\"MA XAS 54\",\"start_date\":\"2022-10-27\",\"end_date_for_non_permanent_staff\":\"2022-10-31\",\"notes\":\"test Request. please ignore\"},\"id\":19001155395,\"created_at\":\"2022-10-06T09:44:13Z\",\"updated_at\":\"2022-10-06T09:44:13Z\",\"quantity\":1,\"stage\":1,\"loaned\":false,\"cost_per_request\":0.0,\"remarks\":null,\"delivery_time\":null,\"is_parent\":true,\"service_item_id\":18}]}";

var json_object = JSON.parse(initial_string); // parse string to json object

var json_string = JSON.stringify(json_object, null, 4); // convert json object to string

gs.info(json_string);

 

Log Output:

 

DevishreeAnant_1-1665138091218.png

 

 

 

@Murthy Ch 

 

When I give the string value statically in background script, target field is getting updated with desired JSON format

 

Code in Background script:

DevishreeAnant_0-1665158555187.png

 

Target field in Incident form:

 

DevishreeAnant_1-1665158589858.png

 

BUT, We cannot give the values like this statically, instead we have to stringify the response body & do the further steps, which indeed isn't giving the desired format

 

please find it below:

Code in Background script:

DevishreeAnant_2-1665158709608.png

 

Target field in Incident form:

DevishreeAnant_3-1665158768611.png

 

Please say what can be done?

TIA