How to parse a JSON response in a Client script coming from script include ?

Subhashree Sub1
Tera Contributor

Hi All,

I am trying to retrieve JSON objects in a client script which receives the JSON response from a Script include through GlideAJAX. However, I am unable to make it work inside the Client script by using JSON.parse and several other ways that I have tried.

Script include:

find_real_file.png

Client Script:

find_real_file.png

Response coming from Script include:

find_real_file.png

 

Please let me know how to retrieve an object from this response in the client script. I am trying to do in onSubmit client script.

 

Thank you!

1 ACCEPTED SOLUTION

_ChrisHelming
Tera Guru

Your JSON object stores the information in an array in the result property. You need to "dot walk" to the result, and to a specific item in the array before you can get the mac address.

alert(answer.result[0].mac_address);

if you think you might ever have more than one result you'll need to loop over the results:

answer.result.forEach(function(infobloxRecord){
   alert(infobloxRecord.mac_address);
   // do something with infobloxrecord.ip_address, .network, ._ref, whatever
}

View solution in original post

9 REPLIES 9

Hi Adrian,

Thank you for your suggestion. Please find my below concern.

I tried this approach and seems like it gives me undefined error in the alert when I am trying get the json object value.

PFB screenshot for reference:

find_real_file.png

Client script:

find_real_file.png

Script include:

find_real_file.png

Response captured in answer :

find_real_file.png

In your case the object is set as string.

use:

var JSONObject = JSON.parse(<the object string>)

Wait, you do that. I see your result is an array of JSON objects

change your alert to:

alert('answer is: ' + answer[0].mac_address);

_ChrisHelming
Tera Guru

Your JSON object stores the information in an array in the result property. You need to "dot walk" to the result, and to a specific item in the array before you can get the mac address.

alert(answer.result[0].mac_address);

if you think you might ever have more than one result you'll need to loop over the results:

answer.result.forEach(function(infobloxRecord){
   alert(infobloxRecord.mac_address);
   // do something with infobloxrecord.ip_address, .network, ._ref, whatever
}

Thanks @Chris Helming , I referred this solution and modified as per my requirement to fix the issue. Thank you!