- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2023 12:21 PM
I am trying to turn a JSON Array into something more readable.
I am populating a Multi line text field.
Example
I have tried by adjusting the client script/script includes, but no luck.
Iam look for some thing like
How can I turn a JSON Array into something more readable?
Thanks for your time - Lon
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2023 01:05 PM
var assetData = [
{"Asset tag":"AQTEST004", "Name":"Shazam", "Serial Number":"LONGABC"},
{"Asset tag":"AQTEST005", "Name":"Someone set up us", "Serial Number":"MINI-X"},
{"Asset tag":"AQG50", "Name":"Gozer", "Serial Number":"DESTRUCT-1"}
];
var output = assetData.map( function(e) {
return "Asset tag: " + e["Asset tag"] + ", Name: " + e.Name + ", Serial Number: " + e["Serial Number"];
} );
gs.info(output.join("\n"));
It's more readable if you use string templates but check first on ServiceNow's support for this:
var output = assetData.map( function(e) {
return `Asset tag:${e["Asset tag"]}, Name:${e.Name}, Serial Number:${e["Serial Number"]}`;
} );
Result:
Asset tag: AQTEST004, Name: Shazam, Serial Number: LONGABC
Asset tag: AQTEST005, Name: Someone set up us, Serial Number: MINI-X
Asset tag: AQG50, Name: Gozer, Serial Number: DESTRUCT-1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2023 01:05 PM
var assetData = [
{"Asset tag":"AQTEST004", "Name":"Shazam", "Serial Number":"LONGABC"},
{"Asset tag":"AQTEST005", "Name":"Someone set up us", "Serial Number":"MINI-X"},
{"Asset tag":"AQG50", "Name":"Gozer", "Serial Number":"DESTRUCT-1"}
];
var output = assetData.map( function(e) {
return "Asset tag: " + e["Asset tag"] + ", Name: " + e.Name + ", Serial Number: " + e["Serial Number"];
} );
gs.info(output.join("\n"));
It's more readable if you use string templates but check first on ServiceNow's support for this:
var output = assetData.map( function(e) {
return `Asset tag:${e["Asset tag"]}, Name:${e.Name}, Serial Number:${e["Serial Number"]}`;
} );
Result:
Asset tag: AQTEST004, Name: Shazam, Serial Number: LONGABC
Asset tag: AQTEST005, Name: Someone set up us, Serial Number: MINI-X
Asset tag: AQG50, Name: Gozer, Serial Number: DESTRUCT-1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2023 01:08 PM
This approach seems to be throwing a number of errors.
const is a reserved word.
Unhandled exception in GlideAjax.
I am not really sure what element or script to apply this to.
The client script or script include?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2023 01:18 PM
Thanks so much for your help and time,
Lon
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2023 01:59 PM
Just curious if you had to do something extra to get it working for you. Might be beneficial to other Community members reading this post.
Another simple way to do it is with stringify's space parameter:
var test = [{"Asset tag":"AQTEST004", "Name":"Shazam", "Serial Number":"LONGABC"},
{"Asset tag":"AQTEST005", "Name":"Someone set up us", "Serial Number":"MINI-X"},
{"Asset tag":"AQG50", "Name":"Gozer", "Serial Number":"DESTRUCT-1"}
];
console.log(JSON.stringify(test, null, 4)); //to add 4 space characters
console.log(JSON.stringify(test, null, "\t")); //to add the tab character
The result would be:
[
{
"Asset tag": "AQTEST004",
"Name": "Shazam",
"Serial Number": "LONGABC"
},
{
"Asset tag": "AQTEST005",
"Name": "Someone set up us",
"Serial Number": "MINI-X"
},
{
"Asset tag": "AQG50",
"Name": "Gozer",
"Serial Number": "DESTRUCT-1"
}
]
It formats it nicely AND it's still in JSON format if you need it as such (although you did mention removing the " characters).