The CreatorCon Call for Content is officially open! Get started here.

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

Hi David,

I have a similar issue whereby, in the script include, I look up a table that has a column for holding a set of name value pairs (it's defined as a 'Name-Value Pairs' type field).

I'd like to return this field's name/value pairs to the calling client script and loop through each entry and be able to reference both the name and the value to build a Json Payload.

At the moment I'm separating the names and values into 2 separate arrays and processing the arrays in the client script but wanted to check if you knew of a more efficient way to do this,

many thanks in advance,

Keiron.

Hello,

I'm not sure why you are separating out the names/values. When you query the column in the AJAX call you can return the stringified JSON object. Then in your client script you can parse the JSON and loop through as desired like so:

for (var key in obj) {
    gs.print(key + ':' + obj[key]);
}

 Hope this helps.

--David

Sukhbir Singh2
Giga Guru

Hi

1. There is a syntax error in the array, but i think its a typo.

2. Try returning a stringified JSON from the backend script include and parse it to JSON in client script.

function getResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
answer = JSON.parse(answer);
var name = answer[0].name;
var member = answer[0].member;
var area = answer[0].area;
alert('name: ' + name+"\nmember: "+member+"\narea: "+area);
}

 

Thanks