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.

How to extract value from body of returned REST Call

bpolo
Tera Guru

We have a REST Call that returns this response:

Account Disabled { "data": {}, "message": "Successful - User has been suspended and accounts disabled.", "status_code": 200 }

The message value will not always be the same, so we want to only display what the value in the message is. Please could someone provide a script on how to strip out the message value from the response.

Thanks in advance! 

 

3 REPLIES 3

Robbie
Kilo Patron
Kilo Patron

Hi @bpolo,

 

You'll want something along the lines of the below:

 

//Sample JSON:
 var responseBody = { "data": {}, "message": "Successful - User has been suspended and accounts disabled.", "status_code": 200 }; //Update this to point to your JSON payload here
//Code:
var message = JSON.stringify(responseBody.message);
gs.print("Message : " + message);
//Result:
//***Script: Message: Successful - User has been suspended and accounts disabled.

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.



Thanks, Robbie 

Thanks very much, Robbie! This was very helpful!

The only piece I had to add was 

 var parsedData = JSON.parse(responseBody);   before I did the stringify statement.
Thanks again!
 

No worries at all @bpolo.

 

To help others (and for me to gain recognition for my efforts), please mark this response correct by clicking on Accept as Solution and/or Kudos.



Thanks, Robbie