Help with JSON returning in client script

Flavio Tiezzi
Kilo Sage

Hello guys,

 

I need to return a script include in a client script. I'm using GlideAjax to do it.
This is the answer that is returning to the client script:

[{"name":"value1",

"member":"111111111", // sys id value

"area":"111111111", // sys id value

},]

 

Return of Glide Ajax in include script:

 

function getResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
answer = JSON.stringify(answer);
var obj = JSON.parse(answer);
alert('obj: ' + obj.area);

 

}

 

What I'm not able to do is get the values of the fields inside the array. It's returning undefined. How can I resolve this?

 

Thanks!

1 ACCEPTED SOLUTION

DScroggins
Kilo Sage

Hello,

The example text is not in the correct format to convert to JSON so assuming it was simply a typo using the following example this is how you'd convert to object and get the value:

Example Array - 

[{"name":"value1",

"member":"111111111", 

"area":"111111111"

}]

Use the following code:

function getResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var obj= JSON.parse(answer);

for(var i in obj){
alert('obj: ' + obj[i].area);
}
 

}

 

If there will only be one item in your returned array then the follow code is cleaner:

function getResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var obj= JSON.parse(answer);


alert('obj: ' + obj[0].area);

 

}

 

Hope this helps.

 

--David

View solution in original post

7 REPLIES 7

DScroggins
Kilo Sage

Hello,

The example text is not in the correct format to convert to JSON so assuming it was simply a typo using the following example this is how you'd convert to object and get the value:

Example Array - 

[{"name":"value1",

"member":"111111111", 

"area":"111111111"

}]

Use the following code:

function getResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var obj= JSON.parse(answer);

for(var i in obj){
alert('obj: ' + obj[i].area);
}
 

}

 

If there will only be one item in your returned array then the follow code is cleaner:

function getResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var obj= JSON.parse(answer);


alert('obj: ' + obj[0].area);

 

}

 

Hope this helps.

 

--David

Hello David,

Your script helped me a lot and I managed to correctly return the JSON. One thing I hadn't put is that I need to return this inside an array, as in the example below. And I have more case inside JSON. But in this script I made, it's returning only one of them.

var answer = response.responseXML.documentElement.getAttribute("answer");
var obj = JSON.parse(answer);

for (var i in obj) {

var rowObj = [];
rowObj.push({
"name": obj[i].name,
"member": obj[i].member.toString(),
"area": obj[i].area.toString(),

});

g_form.setValue('field_name', JSON.stringify(rowObj));

 

If I understand your need you want to push the values from the returned object into a new array then return that array to your form?

 

If so you can loop through all items in the object then push to array are you are doing yet only return the array once the loop is complete like so:

var answer = response.responseXML.documentElement.getAttribute("answer");
var obj = JSON.parse(answer);

var rowObj = [];

for (var i in obj) {

rowObj.push({
"name": obj[i].name,
"member": obj[i].member.toString(),
"area": obj[i].area.toString(),

});

}

g_form.setValue('field_name', JSON.stringify(rowObj));

 

Let me know if I understood correctly and if this helped.

 

--David

Hi David,

This solved my problem! Thank you very much!