- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-13-2022 03:47 AM
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:
Client Script:
Response coming from Script include:
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!
Solved! Go to Solution.
- Labels:
-
Integrations
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-13-2022 05:13 AM
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
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-13-2022 04:19 AM
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:
Client script:
Script include:
Response captured in answer :
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-13-2022 04:54 AM
In your case the object is set as string.
use:
var JSONObject = JSON.parse(<the object string>)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-13-2022 04:58 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-13-2022 05:13 AM
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
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-18-2022 10:38 AM
Thanks