Undefined Value From RESTResponseV2

Diorella Angulo
Tera Expert

Hello, I hope someone can help me.

I'm creating a business rule to get values from a REST Message. Let's say that I want "DescriptionSummary" value, so I have the following: 

 

5-15-2023 11-39-10 AM.jpg

 

I'm getting undefined for the value I want to pull.

5-15-2023 11-41-20 AM.jpg

 

This is my business rule script:

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
try { 
 var r = new sn_ws.RESTMessageV2('Windchill CN', 'Default GET');
 r.setStringParameterNoEscape('customvalue', 'ChangeNotices?$filter=Number%20eq%20%272369991%27');

 var response = r.execute();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();
	
gs.addInfoMessage("http status: " + httpStatus);

var parsed = JSON.stringify(responseBody);
 
var field3 = parsed.Name;
gs.addInfoMessage(" field3--" + field3);
	
this.json = new global.JSON().decode(responseBody);

gs.addInfoMessage("Test: " + this.json.DescriptionSummary);
 
// if (response.getStatusCode() != 200) {
//   gs.info('Bad Request');
// }

var headers = response.getAllHeaders();
for(var i in headers){
  gs.addInfoMessage(headers[i].name + ': ' + headers[i].value);
}
	
gs.addInfoMessage(" Response--" + response);
gs.addInfoMessage(" Response Body--" + responseBody);
	
//var obj = JSON.parse(responseBody);
//var obj = gs.xmlToJSON(responseBody);
//var xmlDoc = new XMLDocument2();
//xmlDoc.parseXML(responseBody);	

//var field3 = xmlDoc.getNodeText("//DescriptionSummary");
//gs.addInfoMessage(" Response--" + responseBody);

//gs.addInfoMessage(" field3--" + field3);	
	
current.update();	

}
catch(ex) {
 var message = ex.message;
}

})(current, previous);

 

I tried multiple ways to get the values but no luck.

 

Thanks!

1 ACCEPTED SOLUTION

Benjamin A
Mega Sage

Could you paste a redacted version of the response body and share the exact value you are trying to access from the response?

 

It looks like the name value you are trying to access is actually located at parsed['value'][0]['Name'] based on what I can see from the screenshots you shared. 

 

Thank you

 

edit:

As @Bharath Kumar A  below mentioned, you need to use JSON.parse instead of JSON.stringify. I missed this when reviewing your code. Good catch @Bharath Kumar A!

 

However, you still will need to include the [0] after parsed.value as the type of parsed.value is an array. So the full code sniped should be

var parsed = JSON.parse(responseBody);

var field3 = parsed.value[0].Name;

View solution in original post

8 REPLIES 8

thomaskennedy
Tera Guru

JSON.stringify() returns a human-readable string, not an obect. So I would expect this to return undefined:

var parsed = JSON.stringify(responseBody);
var field3 = parsed.Name;
gs.addInfoMessage(" field3--" + field3);

 

Benjamin A
Mega Sage

Could you paste a redacted version of the response body and share the exact value you are trying to access from the response?

 

It looks like the name value you are trying to access is actually located at parsed['value'][0]['Name'] based on what I can see from the screenshots you shared. 

 

Thank you

 

edit:

As @Bharath Kumar A  below mentioned, you need to use JSON.parse instead of JSON.stringify. I missed this when reviewing your code. Good catch @Bharath Kumar A!

 

However, you still will need to include the [0] after parsed.value as the type of parsed.value is an array. So the full code sniped should be

var parsed = JSON.parse(responseBody);

var field3 = parsed.value[0].Name;

Yes. I didn't observe [[ in your response. Please mark it as helpful.

Thank you so much! It is showing now the value.