Parse JSON object in UI Page

Martin Pavlov
Tera Expert

Hello,

I've been searching all day how can I parse a JSON object in a UI page. However none of the solutions I've found worked for me.

The case is the following:

1) I have a UI action that opens a modal. In the script I am calling a GlideAjax to ScriptInclude

2 ) The ScriptInclude make a GlideRecord request and return stringified data.

3) The script from point 1) parse the data in the callback. The parsed data is an array of objects. The array is passed to the UI page with this code: 

var dialog = new GlideModal('manager_dialog', false, 600);
dialog.setTitle('Manage Visibility');
dialog.setPreference('data', data); // the data is the array from the script include
dialog.render();

4) In the UI page I want to use the array, loop over it, and display the information.

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<g:evaluate>
var response = RP.getWindowProperties().get('data') // this should be the array from the script include
var parsedArray= JSON.parse(response);
</g:evaluate>
<j:forEach items="${parsedArray}" var="jvar_param">
<div>
<div>
<p>${jvar_param.name}</p>
</div>
<div>
<select>
<option>Private</option>
<option>Public</option>
</select>
</div>
</div>
</j:forEach>
</j:jelly>

 

Unfortunately, the data doesn't appear. If I pass the 'response' variable to the forEach tag, what I see is [object Obejct], [object Obejct] which appears to be a string. However, even parsing the data, the 'items' attribute does not seem to have access to the array.

I will be grateful for any kind of help! 

Thanks!

1 ACCEPTED SOLUTION

Martin Pavlov
Tera Expert

Thank you Ashutosh and Ankur, but those options did not work. I think that the problem was in the rest of my code. I managed to do it this way: 

1) I use GlideModal and pass the id I need with setPreference()

<g:evaluate jelly="true" object="true" var="jvar_target_id">
var response = RP.getWindowProperties().get('target_id');
response;
</g:evaluate>

 

2) Then instead of calling the ScriptInclude in the UI action, I call it here and store the object in the variable


<g:evaluate jelly="true" object="true" var="jvar_data" >
var utils = new PrivateDataUtils();
var result = utils.getRelated('${jvar_target_id}')
var data = JSON.parse(result);
data;
</g:evaluate>

3) Then I loop over the data with j:forEach

 

Thank you once again for the help!

View solution in original post

8 REPLIES 8

Ashutosh Munot1
Kilo Patron
Kilo Patron

Hi,

Can you try this:

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<g:evaluate>
var response = RP.getWindowProperties().get('data') // this should be the array from the script include
var parsedArray= JSON.parse(response);
</g:evaluate>
<j:forEach items="$[parsedArray]" var="jvar_param">
<div>
<div>
<p>${jvar_param.name}</p>
</div>
<div>
<select>
<option>Private</option>
<option>Public</option>
</select>
</div>
</div>
</j:forEach>
</j:jelly>


Thanks,
Ashutosh

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI,

Or Something like this:

<j:forEach begin="0" end="${parsedArray.getSize()-1}" indexVar="jvar_param">


Thanks,
Ashutosh

Martin Pavlov
Tera Expert

Nop, both options do not work 😕