Accessing / Iterating JSON response array

MesihasAle
Tera Contributor

I need to access/iterate an Array from a response so I perform a request:

 

 

function onChange(control, oldValue, newValue, isLoading) {

		var grl = new GlideAjax('global.getRoomLocation');// script include
		grl.addParam('sysparm_name', 'getRoom'); //Method
		grl.addParam('campus',campusRecord); //Parameters	
		grl.getXMLAnswer(getResponse);
	}
}

function getResponse(response){
	console.log(response);

 

 

Script include processing request:

 

 

		var rooms = [];
		while (grParent.next()) {
			rooms.push({
			sysID: grParent.getValue('sys_id'),
			name: grParent.getValue('name'),
			parent: grParent.getValue('parent'),
			locationType: grParent.getValue('cmn_location_type')
			});
		}

		return JSON.stringify(rooms);

 

 

This is the console log of the response I get (so data is coming)

 

 

[{"sysID":"6a14c914dbfa96464650617305961902","name":"Armidale - Taylor Street","parent":null,"locationType":"campus"}]

 

 

 

Now: I need to access the key and values and use them to populate another dropdown list or lets say simply console log them. I've tried in many many different ways but does not seem is working, all I get is undefined or Unhandled exception in GlideAjax.

 

I have tried in many... many different ways but here is a tiny part of what I have tried with no luck.

 

 

function getResponse(response){

	console.log(response);
	console.log(response[0].name);
	console.log(response[name]);

	for (i=0; i<response.length; i++){
		console.log(response[0]['name']);
	}

	for (var key in response){
 		if(obj.hasOwnProperty(key)){
    		console.log(response[key]);
  		}
	}

   for(var loop = 0 ; loop < response.length;loop++) {
		alert(response[loop].sysID);
   }

 

 

only the first console.log works and prints this( again):

 

[{"sysID":"6a14c914dbfa96464650617305961902","name":"Armidale - Taylor Street","parent":null,"locationType":"campus"}]

 

 

 

please advice

 

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi @MesihasAle ,

 

It looks like you are receiving a JSON string from your GlideAjax call and trying to access the values directly as if response were an object array. However the response is actually a JSON string, you need to parse it first to convert it into a JavaScript object before you can iterate over it or access its properties.

Here's how you can fix your getResponse function to handle the JSON parsing:

function getResponse(response) {
    // First, parse the JSON string into an array of objects
    var response = JSON.parse(response);
    
    console.log(response); // This will now show the actual array of objects

    // Looping through the array using a for loop
    for (var i = 0; i < response.length; i++) {
        console.log(response[i].name); // Correctly accessing each name property
    }

    // Using a for-in loop to iterate over array indices (not typically recommended for arrays)
    for (var index in response) {
        if (response.hasOwnProperty(index)) {
            console.log(response[index].name);
        }
    }

    // Using a for loop to display alerts (if needed)
    for (var loop = 0; loop < response.length; loop++) {
        alert(response[loop].sysID);
    }
}

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar

View solution in original post

1 REPLY 1

Community Alums
Not applicable

Hi @MesihasAle ,

 

It looks like you are receiving a JSON string from your GlideAjax call and trying to access the values directly as if response were an object array. However the response is actually a JSON string, you need to parse it first to convert it into a JavaScript object before you can iterate over it or access its properties.

Here's how you can fix your getResponse function to handle the JSON parsing:

function getResponse(response) {
    // First, parse the JSON string into an array of objects
    var response = JSON.parse(response);
    
    console.log(response); // This will now show the actual array of objects

    // Looping through the array using a for loop
    for (var i = 0; i < response.length; i++) {
        console.log(response[i].name); // Correctly accessing each name property
    }

    // Using a for-in loop to iterate over array indices (not typically recommended for arrays)
    for (var index in response) {
        if (response.hasOwnProperty(index)) {
            console.log(response[index].name);
        }
    }

    // Using a for loop to display alerts (if needed)
    for (var loop = 0; loop < response.length; loop++) {
        alert(response[loop].sysID);
    }
}

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar