How to parse this JSON Response and set the value in the Variable of catalog form

Shree14
Tera Contributor

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

2 ACCEPTED SOLUTIONS

Sandeep Rajput
Tera Patron
Tera Patron

@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.

View solution in original post

@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.

 

Untitled.jpg

Please don't forget to mark my answer helpful and correct.

View solution in original post

6 REPLIES 6

Sandeep Rajput
Tera Patron
Tera Patron

@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.

Thank You, @Sandeep Rajput !

The solves my query, and thanks for the Explanation.

Shree14
Tera Contributor

Hi @Sandeep Rajput 

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 : 

{"suggestions": [ {"street_line": "1042 W Center St", "secondary": "Ofc", "city": "Orem,"state": "UT","zipcode""84057",
"entries"1}, {"street_line""1042 W Center St", "secondary""", "city""Alma","state""MI", "zipcode": "48801", "entries": 0} ] }
 
How to return this Array as Multiple suggestion in a Variable.
 
Regards,
Shree
      
        
           
   

@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.

Screenshot 2023-04-25 at 12.16.32 AM.png

 

Here is how the final result is

Screenshot 2023-04-25 at 12.16.19 AM.png