How can I turn a JSON Array into something more readable?

Lon Landry4
Mega Sage

I am trying to turn a JSON Array into something more readable.

I am populating a Multi line text field.

Example
parse.png

I have tried by adjusting the client script/script includes, but no luck.

Iam look for some thing like
parse2.png

How can I turn a JSON Array into something more readable?

 

Thanks for your time - Lon

1 ACCEPTED SOLUTION

thomaskennedy
Tera Guru
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

 

View solution in original post

4 REPLIES 4

thomaskennedy
Tera Guru
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

 

Lon Landry4
Mega Sage

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?

Lon Landry4
Mega Sage

Thanks so much for your help and time,

Lon

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).