Scripted Rest API returns[object Object] how to parse ?

Kiddy
Tera Guru

Couldnt parse [object Object]

 

instance A:

rest api body is set

var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://devxxxxx.service-now.com/api/757899/validatepredeploy');
request.setHttpMethod('POST');


var enc=current.u_enclave;

var user = 'admin';
var password = 'xxxxx';

var body ={};
body.enclave=enc;

request.setBasicAuth(user, password);
request.setRequestHeader("Accept", "application/json");
request.setRequestHeader('Content-Type', 'application/json');
request.setRequestBody(JSON.stringify(body));

var response = request.execute();

 

Instance B: scripted Rest API parsing to get value of enclave

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

// implement resource here

var requestBody = request.body.data;
gs.log('requestJSON enclave 1'+requestBody ,'sh01');
})(request, response);

 

Ouput:

requestJSON enclave 1[object Object]

8 REPLIES 8

Anirudha1
Giga Contributor

Hi Kiddy,

Just use gs.log('requestJSON enclave 1 '+JSON.stringify(requestBody)); 

The body is a JSON object so you will need to convert it to string to see it in the log.

Please mark Helpful if it solves your query.

Hi @Anirudha ,

 

Thanks 

i got below output ,but how can i get the value of key name enclave?

 

find_real_file.png

Hi Kiddy,

If the value of requestBody is {"enclave":{}}, parse requestBody.

var jsonObj = JSON.parse(requestBody);

To see the value of Json object, use JSUtil.logObject().

https://developer.servicenow.com/dev.do#!/reference/api/rome/server_legacy/c_JSUtilAPI#r_JSUtil-logO...

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

// implement resource here

var requestBody = request.body.data;
//gs.log('requestJSON enclave 1'+requestBody ,'sh01');

var jsonObj = JSON.parse(responseBody);
JSUtil.logObject(jsonObj, 'sh01');
var enclave = jsonObj.enclave;
JSUtil.logObject(enclave, 'enclave');

})(request, response);

Updates code:


var requestBody = request.body.data;


gs.log('requestJSON enclave 1 '+JSON.stringify(requestBody),'sh02');