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.

Unable to retrieve the number value from JSON response

Kumar147
Tera Contributor

Hi,

I am trying to load the data into a table by getting Response body from another instance. Anyhow i am able to get the response, but unable to retrieve the valve. its getting undefined. 

response is : {"result":[{"number":"CHG0030779"}]}

 

script using:

    var pagedR = new sn_ws.RESTMessageV2('Change Data', 'Change Request');
    var response = pagedR.execute();
    var responseBody = response.getBody();
    var httpStatus = response.getStatusCode();
    var prsData = JSON.parse(responseBody);
gs.log('Test Number: '+prsData.result.number);
 
Please help me in this. Thank you
4 REPLIES 4

Sandeep Rajput
Tera Patron
Tera Patron

@Kumar147 Result Json contains an array of object hence you need to use an index to access individual object. Here is an updated script from my side.

 

    var pagedR = new sn_ws.RESTMessageV2('Change Data', 'Change Request');
    var response = pagedR.execute();
    var responseBody = response.getBody();
    var httpStatus = response.getStatusCode();
    var prsData = JSON.parse(responseBody);
gs.log('Test Number: '+prsData.result[0].number); //Access the number property of object at 0th index.

For accessing all the objects in an array you would need to use for or while loop.

 

Hope this helps.

Hello @Sandeep Rajput 

Thank you for your response. Response body is so big and contains multiple records. I am actually trying to get all change requests and to load the data into a table. I need the number for the query. How can we get the number value from multiple requests.

@Kumar147 Did you try to use 

gs.log('Test Number: '+prsData.result[0].number);

I am able to get the number. but when I am trying to insert the assignment group data, i am getting link instead of assignment group name. how can we overcome that.