The Zurich release has arrived! Interested in new features and functionalities? Click here for more

How to read a value from an API response

gnewuser
Tera Contributor

Hi

I am making a REST call to an external system and based on what is the value need to create a manual task for a group.

 

Below is the response back that I get in ServiceNow and I need to create a task only if the "Status" has  a value in it (which signals that there is an error )

 

{"status":null,"requestID":null,"warnings":null,"errors":["Status : failed\.\n"],"retryWait":0,"metaData":null,"attributes":null,"complete":false,"failure":false,"success":false,"retry":false}

 

How do I read the value of status in my script in workflow? 

 

Thanks

1 ACCEPTED SOLUTION

Try 

var response = r.execute();
var responseBody = JSON.parse(response.getBody());

gs.log(responseBody.errors);

View solution in original post

4 REPLIES 4

Mike_R
Kilo Patron
Kilo Patron

set a variable equal to the response such as

 

var response = {"status":null,"requestID":null,"warnings":null,"errors":["Status : failed\.\n"],"retryWait":0,"metaData":null,"attributes":null,"complete":false,"failure":false,"success":false,"retry":false};

 

then you can read the errors using response.errors

 

example

 


if(response.errors){
//there is an error
gs.log("there is an error");
}

else{
//there isn't an error
gs.log("there isn't an error");
}

gnewuser
Tera Contributor

Thanks so much Mike_R for responding. But I still get undefined when I do the response.errors.

 

Actually this is what I have 

 

var response = r.execute();
var responseBody = response.getBody();

gs.log(responseBody.errors) -> shows undefined.

 

Try 

var response = r.execute();
var responseBody = JSON.parse(response.getBody());

gs.log(responseBody.errors);

gnewuser
Tera Contributor

Thanks so much. That worked!