We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

Unable to parse result from response body

clyon
Tera Guru

I have a REST message. I'm getting the data back in the response (see below). But when I try to parse the fields out so I can update the fields on my form, I get undefined.

What am I missing?

var response = restMessage.execute();

gs.log(response.getBody());

//{"result":[{"u_segment":"Regional","u_account_owner":"Denise Bond","name":"123 MONEY LLC","u_region":"NE"}]}

 

var str = response.getBody();

var parser = new JSONParser();  

//var parsedResponse = response.parser(str);  

var parsedResponse = parser.parse(str);

gs.log(parsedResponse.result.name);

 

//Set values returned to incident form

current.u_account_name = parsedResponse.result.name;

current.u_account_owner = parsedResponse.result.u_account_owner;

current.u_region = parsedResponse.result.u_region;

current.u_segment = parsedResponse.result.u_segment;

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron

Hi Christina,



Following script will give you the desired values:


You missed the array of object. the values are present in result[0]



var response = restMessage.execute();


var str = response.getBody();


var parser = new JSONParser();


var parsedData = parser.parse(str);


current.u_account_name = parsedData.result[0].name;


current.u_account_owner = parsedData.result[0].u_account_owner;


current.u_region = parsedData.result[0].u_region;


current.u_segment = parsedData.result[0].u_segment;



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


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

View solution in original post

17 REPLIES 17

Khozema Attar1
Tera Guru

Hi,



Can you try with



var str = response.getBody();


var parser = new JSONParser();  


//var parsedResponse = response.parser(str);  


var parsedResponse = parser.parse(str);


gs.log(parsedResponse.result.name);



//Set values returned to incident form


current.u_account_name = parsedResponse[0].result.name;


current.u_account_owner = parsedResponse[1].result.u_account_owner;


current.u_region = parsedResponse[2].result.u_region;


current.u_segment = parsedResponse[3].result.u_segment;



Something like this



Thanks,


Hi Khozema,



That won't work i.e. parsedResponse[0].result.name the values are present in the result array and not in the initial level of json body.



screenshot below:



json-body.JPG



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


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

Hi Ankur,



Ohh i get it.



Thanks