The CreatorCon Call for Content is officially open! Get started here.

Retrieve value from Response.getBody() in JSON

manish123
Giga Guru

Hi All,

In REST integration, we are getting Response.getBody() with lots of fields and values respective to it. However, i would like to get some specific value from that Response.Could anyone please let me know how to get that? I tried with decode and Parse but still in log its displaying undefined.

1 ACCEPTED SOLUTION

Result does not appear to be an array. I'm reaching this as:



gs.log('Number=' + jsonObject.result.number);


View solution in original post

42 REPLIES 42

Hi ctomasi



I have a very similar issue.   I;m sending ticket info to a 3rd party and I get a response of:


{"message":"saved successfully.","ticketId":4920506}


I need the ticketId only to be updated to a field called u_vendor_ref_number


In the business rule I have this at the end of the call to the 3rd party


var response = request.execute();


gs.log('xxx API Status v4: ' +response.getStatusCode());


gs.log('xxx API Response v4: ' +response.getBody());     // this logs returns 'xxx API Response v4: {"message":"saved successfully.","ticketId":4920506}'



So I tried adding like the above example the following


var jsonBody = response.getBody();


var jsonObject = JSON.parse(jsonBody);


gs.log('Number=' + jsonObject.result.ticketId); // this logs returns 'Number=undefined'


How can I get the tickedId reference and add it to the u_vendor_ref_number from the business rule.


Hi Dave,



Based on what you've shown, your JSON response doesn't have a 'result' object within it. Try this instead.



var jsonBody = response.getBody();


var jsonObject = JSON.parse(jsonBody);


gs.log('Number=' + jsonObject.ticketId);



More info on this can be found in episode 37 of TechNow where we talked specifically about parsing JSON.


TechNow Episode List


Thanks Chuck, that did the trick but now having issue getting that ticketId into my ref field.   Had a quick look through the YT video you mentioned but it was all in babackgorund scripts so you didn't seem to touch on doing a current update from a business rule


See my comment on your other thread, Dave.


Hi Chuck/Dave,

I do have same query. Is it not possible to set any field from json object similar to below

var jsonObject = JSON.parse(responseBody);
current.setValue('u_summary', jsonObject.fields.summary);

 

I'm able to see below in syslog table, but i would like to set this value in to a field in the current record as mentioned above which is not working.

gs.info('Summary='+jsonObject.fields.summary);

 

Please suggest