Format JSON Server Side

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2017 09:33 AM
I am trying to format a JSON string for logging. I have a client script where I use JSON.stringify(content, null, 4) and I get a nice formatted JSON string.
Is there an equivalent function available server side? Also I seem to be able to get server side is one long string with no formatting.
{"generalInfo":{"actionType":"SC","dependencyType":null,"env":"tec","filterOverride":null,"guid":"6851370928697643","jobType":"j","jobfamily":"TST_TST","numRecords":151,"objectIdentifier":null,"requestType":"CP","subActionType":"cancel","userId":"mark.didrikson","vendor":"serviceNow","workstation":null},
I want it to look like this:
{
"generalInfo": {
"actionType": "PDS",
"dependencyType": null,
"env": "tec",
"filterOverride": null,
"guid": "1424831054435801",
"jobType": "s",
"jobfamily": "TST_TS5",
"numRecords": 10,
"objectIdentifier": null,
"requestType": "CP",
"subActionType": "subOnHold",
"userId": "mark.didrikson",
"vendor": "serviceNow",
"workstation": null
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2021 06:57 AM
server-side code (Global)
var testObject = { x: 5, y: 6 };
JSON.stringify(testObject, null, ' ');
output
{
"x": 5,
"y": 6
}
reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-31-2021 01:35 AM
I've executed the following code using Scripts - Background (i.e. on server side)
var initial_string = '{"generalInfo":{"actionType":"SC","dependencyType":null,"env":"tec","filterOverride":null,"guid":"6851370928697643","jobType":"j","jobfamily":"TST_TST","numRecords":151,"objectIdentifier":null,"requestType":"CP","subActionType":"cancel","userId":"mark.didrikson","vendor":"serviceNow","workstation":null}}';
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);
Execution result:
*** Script: {
"generalInfo": {
"actionType": "SC",
"dependencyType": null,
"env": "tec",
"filterOverride": null,
"guid": "6851370928697643",
"jobType": "j",
"jobfamily": "TST_TST",
"numRecords": 151,
"objectIdentifier": null,
"requestType": "CP",
"subActionType": "cancel",
"userId": "mark.didrikson",
"vendor": "serviceNow",
"workstation": null
}
}