- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2023 08:51 AM
HI All,
Please let me know where I'm going wrong while retrieving the Parsed value.
This is the Response : [{ "input_index":0, "candidate_index":0,"delivery_line_1":"3214 abc" }]
I'm trying to parse this response and wants to put the value of delivery_line_1 into the address field of a catalog form.
The script used below is the Onchange Script:
var ga = new GlideAjax('ScriptInclude');
ga.addParam('sysparm_name', 'getStreet'); //Method
ga.addParam('sysparm_street',street);
ga.getXML(ajaxResponse);
function ajaxResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer); //Alert is working fine.
var obj = JSON.parse(answer); // Parsing
g_form.setValue('address_1', obj[0].input_index); // Obj[0] is working fine
g_form.setValue('address_2', obj[2].delivery_line_1); //this Delivery line_1 value not populating in the address 2 & I'm getting "Javascript Console Error"
}
Regards,
Shree
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2023 09:14 AM
@Shree14 Here is the updated code.
var ga = new GlideAjax('ScriptInclude');
ga.addParam('sysparm_name', 'getStreet'); //Method
ga.addParam('sysparm_street',street);
ga.getXML(ajaxResponse);
function ajaxResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer); //Alert is working fine.
var obj = JSON.parse(answer); // Parsing
g_form.setValue('address_1', obj[0].input_index); // Obj[0] is working fine
g_form.setValue('address_2', obj[0].delivery_line_1); //Now the delivery line should populate
Explanation: Obj is an array of Objects. Since your array only contains a single object [{ "input_index":0, "candidate_index":0,"delivery_line_1":"3214 abc" }] hence you need to use 0th index to access its properties.
Hence in order to access different properties you access them like following.
var input_index = obj[0].input_index;
var candidate_index = obj[0].candidate_index;
var delivery_line_1 =obj[0]. delivery_line_1;
Had there been more than 1 object in array it would have looked like the following.
[{ "input_index":0, "candidate_index":0,"delivery_line_1":"3214 abc" },{ "input_index":2, "candidate_index":0,"delivery_line_1":"abc abc" }];
Here you need to access object at index one like following
var input_index = obj[1].input_index;
var candidate_index = obj[1].candidate_index;
var delivery_line_1 =obj[1]. delivery_line_1;
Hope this clarifies the confusion.
Please mark my answer correct if it addresses your issue.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2023 12:43 AM
@Shree14 This is how your script should be modified.
function onLoad() {
//Type appropriate comment here, and begin script below
var jsonString = '{"suggestions": [{"street_line": "1042 W Center St", "secondary": "Ofc", "city": "Orem","state": "UT","zipcode": "84057","entries": 1}, {"street_line": "1044 W Center St", "secondary": "", "city": "Alma","state": "MI", "zipcode": "48801", "entries": 0}]}';
var jsonObj = JSON.parse(jsonString);
var addressArray = jsonObj.suggestions;
for (var i=0;i<addressArray.length;i++) {
var completeAddress = addressArray[i].street_line+ ', '+addressArray[i].secondary+ ', '+addressArray[i].city+ ', '+addressArray[i].state+ ', '+addressArray[i].zipcode;
g_form.addOption('address',completeAddress, completeAddress);
}
}
This is how the end result looks.
Please don't forget to mark my answer helpful and correct.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2023 12:16 AM - edited 04-25-2023 12:45 AM
Thanks for the Response, and the solution you gave works.
But we are not only looking for Street_line as option. Instead, whole object in the Array should become an option for Address variable.
currently I'm using Onchange Script : onchange of variable - 'address' > the Options should add to the variable - 'shipping_address'.
Also if the value in 'address' changes > the options in 'shipping_address' should clearout and the new values should populate in option after changing the input in 'address'.
Address>>
Option 1 : 1042 W Center St, Ofc, Orem, UT - 84057
{"street_line": "1042 W Center St", "secondary": "Ofc", "city": "Orem","state": "UT","zipcode": "84057","entries": 1}
Option2: 1044 W Center St, Alma, MI - 48801
{"street_line": "1044 W Center St", "secondary": "", "city": "Alma","state": "MI", "zipcode": "48801", "entries": 0}
In the image, 32 Abc is the old option it should clear out.
Please let me know if we can Achieve this way.
Thanks,
Shree

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2023 12:43 AM
@Shree14 This is how your script should be modified.
function onLoad() {
//Type appropriate comment here, and begin script below
var jsonString = '{"suggestions": [{"street_line": "1042 W Center St", "secondary": "Ofc", "city": "Orem","state": "UT","zipcode": "84057","entries": 1}, {"street_line": "1044 W Center St", "secondary": "", "city": "Alma","state": "MI", "zipcode": "48801", "entries": 0}]}';
var jsonObj = JSON.parse(jsonString);
var addressArray = jsonObj.suggestions;
for (var i=0;i<addressArray.length;i++) {
var completeAddress = addressArray[i].street_line+ ', '+addressArray[i].secondary+ ', '+addressArray[i].city+ ', '+addressArray[i].state+ ', '+addressArray[i].zipcode;
g_form.addOption('address',completeAddress, completeAddress);
}
}
This is how the end result looks.
Please don't forget to mark my answer helpful and correct.