Need Group Name instead of Sys Id in JSON query

SN Rookie
Giga Expert

Hi All,

I'm fetching a simple API query which is returning the group details of specific user. However, the group name is being returned as a sys_id instead of display name. How can I achieve that?

My REST Query is as below - 

https://<instancename>.service-now.com/api/now/table/sys_user_grmember?sysparm_query=group.typeLIKE7bba903b0a0aa01e007536d6d0711429%5Egroup.active%3Dtrue&user.user_name=a376625

 

1 ACCEPTED SOLUTION

Sanbir Singh1
Kilo Guru

Just add the below line of code in the API and it should work fine - 

 

&sysparm_fields=group.name

View solution in original post

9 REPLIES 9

I see, how can I modify it so that it should return the actual title in JSON?

Hi 

Maybe you need to do it via the code. Something like getDisplayValue(). But now if you want to reflect the changes in URL it won't as it is a API the will refer the table and it needs the sys_id for the same.

I think you need to grab the data as XML because then you will have something like 

<group display_value="group_name">sys_id</group>

 

But if you want to work with it as JSON then you need to convert it.
That can be done with by:

	var xmlStr = '<?xml version="1.0" encoding="UTF-8"?>' +
	'<record>' +
	'<short_description>test incident</short_description>' +
	'<caller_id display_value="tester">c4dc9e61dbbc2300b182793ebf96198f</caller_id>' +
	'</record>';
	
	var xmlHelper = new XMLHelper(xmlStr);
	
	var obj = xmlHelper.toObject();
	
	gs.print(JSON.stringify(obj, null, 2));
	
	/*Prints out:
 	*** Script: {
		"short_description": "test incident",
		"caller_id": {
			"@display_value": "tester",
			"#text": "c4dc9e61dbbc2300b182793ebf96198f"
		}
	}
	*/

 

Then you have the data you want.
You can also work dirctly with the recieved XML

Just a samle:

	var xml = '<results>' +
	'<result>' +
	'<row>' +
	'<nodetest1>test1</nodetest1>' +
	'<nodetest2>test2</nodetest2>' +
	'</row>' +
	'<row>' +
	'<nodetest3>test3</nodetest3>' +
	'<nodetest4 display_value="tester">test4</nodetest4>' +
	'</row>' +
	'</result>' +
	'</results>';
	
	var count = 1;
	
	//Parsing XML string to a xmlDoc
	var xmlGrDoc = new XMLDocument2();
	xmlGrDoc.parseXML(xml);
	
	//Gets the parent node
	var nodes = xmlGrDoc.getNode('//result');
	var iter = nodes.getChildNodeIterator();
	
	
	
	while(iter.hasNext()){
		
		//Loops through each node
		var n = iter.next();
		
		var _row = '';
		_row += count + '\n';
		//Lets see if its a parent
		var rec = n.getChildNodeIterator();
		//If parent
		while(rec.hasNext()){

			//Lets get our row
			var row = rec.next();
			//Info for each row
			_row += 'Node Name ' + row.getNodeName() + ' - Node Value ' + row.getTextContent() + 'att = ' + (row.getAttribute('display_value') || 'no attribute') + '\n';
			
			//gs.info('Node name: ' +   row.getNodeName());
			//gs.info('Node Text Content (values): ' + row.getTextContent());
			//gs.info('Node value (xml string): ' + row);
			
		}
		gs.info(_row);
		count++;
	}

SN Rookie
Giga Expert

I just added the below line of code in the API and it works fine now - 

 

&sysparm_fields=group.name

Sanbir Singh1
Kilo Guru

Just add the below line of code in the API and it should work fine - 

 

&sysparm_fields=group.name