Convert Json into string is working in background script but not working in scheduled job

lucky24
Tera Contributor

Hi Team,

I am getting response in Json then I am converting into string.

As per below image I want to fatch test id and it is giving me result

find_real_file.pngfind_real_file.png

 

Same configuration I am doing in scheduled job but there I am not getting result

 

find_real_file.png

 

Can someone help me here?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

this should work fine

var response1 = r1.execute();
var responseBody1 = response1.getBody();
var httpStatus1 = response1.getStatusCode();

gs.info("test" + responseBody1);
var parsedData = JSON.parse(responseBody1);
gs.info(" test:" + parsedData.data[0].id);

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Maik Skoddow
Tera Patron
Tera Patron

Hi

please always use only the two following standard methods for converting JSON to String and vice-versa

  • JSON.stringify()
  • JSON.parse()

Kind regards
Maik

Hi Maik,

Now I am using like below code-

    var response1 = r1.execute();
    var responseBody1 = response1.getBody();
    var httpStatus1 = response1.getStatusCode();
   

    gs.info("test" + responseBody1);
    var str = JSON.stringify(responseBody1);
    var parser = new JSONParser();
    var parsedData = parser.parse(str);
    gs.info(" test:" + parsedData.data[0].id);

 

But still i am not getting result.

 

In responseBody1 I am getting response like - {"data":[{"displayName":"TEST","id":117}]}

 

How can i solve it?

Hi @lucky 

as I see @Ankur Bawiskar was faster than me and I agree with him. My solution would be the same.

Please let us know whether his solution proposal works for you!

Kind regards
Maik

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

this should work fine

var response1 = r1.execute();
var responseBody1 = response1.getBody();
var httpStatus1 = response1.getStatusCode();

gs.info("test" + responseBody1);
var parsedData = JSON.parse(responseBody1);
gs.info(" test:" + parsedData.data[0].id);

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader