- 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-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-20-2023 10:07 AM
Thank You, @Sandeep Rajput !
The solves my query, and thanks for the Explanation.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2023 12:47 AM - edited 04-24-2023 06:21 AM
I have one more query with returning the Array: through API geeting an Array response with multiple addresses. I want to return this Response as Suggestions to End user.
Ex: searching a value would return multiple addresses coming in the API response. Same should be availabe as a Drop down / suggestion to the End user while submitting the Catalog form
Response :

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2023 12:00 PM
@Shree14 first of all the JSON you shared was slightly incorrect "Orem,"state" here , should have been "Orem", "state"
Anyways, here is how you should parse the values in an onLoad Client script.
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++) {
g_form.addOption('address', addressArray[i].street_line, addressArray[i].street_line);
}
}
Here is how the client script is configured.
Here is how the final result is