Getting Values from JSON Payload

Chad R
Tera Expert

Hi All,

 

Working on getting some values form a third party app, below is the code I have so far just a small test trying to get to the description value in this json that is shown below:

 

 

 

"ReceiveMessageResponse": {
"ResponseMetadata": {
"RequestId": "0bdc413b-0432-59c"
},
"ReceiveMessageResult": {
"messages": [
{
"MessageId": "62c8c3e1-fc3f-484c",
"ReceiptHandle": "AQEBCIoDITr8g+wmQZeBDrW/tMrdiPEZj4mRv3B/+sox4yo31M9Pi6VeOnyfSHwU5+lLkv3FF3mfOpNdEAyuXdac9qYOwlV4Fqqtc4bP3GeW35J7k",
"MD5OfBody": "e6365f3fba247",
"Body": {
"clientName": "test client",
"ticketType": "Change",
"clientTicketNumber": "test423423",
"ticketNumber": "test234234",
"openedAt": "2024-02-16T14:50:28Z",
"updatedOn": "2024-02-19T14:17:22Z",
"requestedByName": "test name",
"shortDescription": "test short description",
"type": "Normal",
"category": "Other",
"approval": "Requested",
"startDate": "2024-02-16T18:00:00Z",
"endDate": "2024-02-24T14:51:06Z",
"risk": "High",
"impact": "High",
"description": "2/16 Normal Change Test \r\n\r\nTesting change - second change ",
"justification": "test",
"preChangeChecks": "test",
"implementationPlan": "test",
"testPlan": "test",
"backoutPlan": "test",
"state": "Authorize",
"priority": "4",
"cis": "test cis",
"group": "test group",
"ensonoGroup": "test group"

 

 I commented out the parser because as I understand it you don't need to the parse the response unless its coming across as text which this doesn't appear to be.

 

try {
    var r = new sn_ws.RESTMessageV2('Get Ensono Messages', 'Default GET');

    var response = r.execute();
    var responseBody = response.getBody();
    var httpStatus = response.getStatusCode();
   //var parser = new JSONParser();
   //var parsed = parser.parse(responseBody);
   //ensure status is 200
    gs.info("Status Code" + " " + httpStatus);
	gs.info("responseBody" + " " + responseBody);

	//declare data points from parced object
    var description = responseBody.ReceiveMessageResponse.ReceiveMessageResult.messages.Body.description;
	
	gs.info(description);

    

} catch (ex) {
    var message = ex.message;
    gs.error("This is the ex error message" + message);
}

 

 

Each time I execute the job description is getting logged as undefined, not sure where I am going wrong any help is MUCH appreciated.

2 REPLIES 2

Anand Kumar P
Giga Patron
Giga Patron

Hi @Chad R ,

Try to parse like below

var responseBody = response.getBody();

var parsedBody = JSON.parse(responseBody);

vardescription=parsedBody.ReceiveMessageResponse.ReceiveMessageResult.messages[0].Body.description; 

 

Mark it as helpful and solution proposed if it serves your purpose.
Thanks,
Anand

Sandeep Rajput
Tera Patron
Tera Patron

@Chad R Update your code as follows.

 

try {
    var r = new sn_ws.RESTMessageV2('Get Ensono Messages', 'Default GET');

    var response = r.execute();
    var responseBody = JSON.parse(response.getBody());
    var httpStatus = response.getStatusCode();
   //var parser = new JSONParser();
   //var parsed = parser.parse(responseBody);
   //ensure status is 200
    gs.info("Status Code" + " " + httpStatus);
	gs.info("responseBody" + " " + responseBody);

	//declare data points from parced object
    var description = responseBody.ReceiveMessageResponse.ReceiveMessageResult.messages[0].Body.description;
	
	gs.info(description);

    

} catch (ex) {
    var message = ex.message;
    gs.error("This is the ex error message" + message);
}

Hope this helps.