Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Parse outbound REST Message JSON response

JJG
Kilo Guru

I have an outbound REST message that gets an 'auth_token' via the JSON response.

I need a script that takes the response, parses it, and captures the 'auth_token' in a variable.

I have built most of the script, but I am running into problems parsing the response.

Here is what I have so far:

try {

    var r = new sn_ws.RESTMessageV2('CRM Dynamics', 'Access Token');
    var response = r.execute();	
    var responseBody = response.getBody();	
    var httpStatus = response.getStatusCode();
    
    gs.info("responseBody: " + responseBody);
    gs.info("responseBody.length: " + responseBody.length);

    for (var i = 0; i < responseBody.length; i++) {
		
        //Capture the access_token value from the JSON response
		
    }
	
} catch (ex) {
    var message = ex.message;
}

 

Output in the logs:

responseBody: {
"token_type":"Bearer",
"expires_in":"3599",
"ext_expires_in":"3599",
"expires_on":"1648143633",
"not_before":"1648139733",
"resource":"test",
"access_token":"jrueion987JKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6ImpTMVhvMU9XRGpfNTJ2YndHTmd2UU8yVnpNYyIsImtpZCI6ImpTMVhvMU9XRGpfNTJ2YndHTmd2UU8yVnpNYyJ9.eyJhdWQiOiJodHRwczovL3VhdC5hcGkuY3JtLmR5bmFtaWNzLmNvbSIsImlzcyI6Imh0dHBzOi8vc3RzLndpbmRvd3MubmV0LzRiMWM2YjU0LTQwMzEtNDgxZS1iOWM4LTkwMDBiNGUzYjJjMi8iLCJpYXQiOjE2NDgxMzk3MzMsIm5iZiI6MTY0ODEzOTczMywiZXhwIjoxNjQ4MTQzNjMzLCJhaW8iOiJFMlpnWUFoeGkvUEoyWlVmc2ZhTWhuSGVNNWZiQUE9PSIsImFwcGlkIjoiZjRhNDY2ZmEtNjY1My00ODdhLThjNDktMzlmNGY1YjQ0NjRlIiwiYXBwaWRhY3IiOiIxIiwiaWRwIjoiaHR0cHM6Ly9zdHMud2luZG93cy5uZXQvNGIxYzZiNTQtNDAzMS00ODFlLWI5YzgtOTAwMGI0ZTNiMmMyLyIsIm9pZCI6ImI1YTkzYTI5LWEwMjktNGQwYS1iOWQxLTUwZmMwN2M0M2YwOCIsInJoIjoiMC5BVVVBVkdzY1N6RkFIa2k1eUpBQXRPT3l3Z2NBQUFBQUFBQUF3QUFBQUFBQUFBQkZBQUEuIiwic3ViIjoiYjVhOTNhMjktYTAyOS00ZDBhLWI5ZDEtNTBmYzA3YzQzZjA4IiwidGlkIjoiNGIxYzZiNTQtNDAzMS00ODFlLWI5YzgtOTAwMGI0ZTNiMmMyIiwidXRpIjoiVXZhSWprU0pwRS1nU19DUldkb0JBQSIsInZlciI6IjEuMCJ9.njF8IjMRndVGwldHsRwEtAvGNTmX12B588XYUVNCbEUkP_4AoOIIxWKi3KxSLVa1PnGYHeS8wdOgRCloKA1iS7NPE4Wppe-es_WXAbVSNU65rXXdMV9SPj1dojOplaxUQFo1TqhV3P2itwHqKdY_EoeoXsBqdwxNOcmr78VCptPI-Y90jzLt7Fn6dP0yFicG6HV6C84I4gSb8pRaU1fveLwvf4vOJuI-gf30Olynaoq9mEWXvMDnkpwfBLGzJYhB6tdFzRRdoW-FmG-ETAW72NdvBRNKp7Exg5ViiKNqNra4iRoyXRkNurkPExdUq_lqJT011fJ29R69hgjfieuryb"
}

 

responseBody.length: 1427

1 ACCEPTED SOLUTION

chrisperry
Giga Sage

Hi there,

You can parse the response body and grab the value directly by using JSON.parse as below:

try {

    var r = new sn_ws.RESTMessageV2('CRM Dynamics', 'Access Token');
    var response = r.execute();	
    var responseBody = response.getBody();	
    var httpStatus = response.getStatusCode();
    
    gs.info("responseBody: " + responseBody);
    gs.info("responseBody.length: " + responseBody.length);

    var accessToken = JSON.parse(responseBody).access_token;
	
} catch (ex) {
    var message = ex.message;
}

If this answer is helpful please mark correct and helpful!

Regards,

Christopher Perry

If this answer is helpful please mark correct and helpful!

Regards,
Chris Perry

View solution in original post

1 REPLY 1

chrisperry
Giga Sage

Hi there,

You can parse the response body and grab the value directly by using JSON.parse as below:

try {

    var r = new sn_ws.RESTMessageV2('CRM Dynamics', 'Access Token');
    var response = r.execute();	
    var responseBody = response.getBody();	
    var httpStatus = response.getStatusCode();
    
    gs.info("responseBody: " + responseBody);
    gs.info("responseBody.length: " + responseBody.length);

    var accessToken = JSON.parse(responseBody).access_token;
	
} catch (ex) {
    var message = ex.message;
}

If this answer is helpful please mark correct and helpful!

Regards,

Christopher Perry

If this answer is helpful please mark correct and helpful!

Regards,
Chris Perry