Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to convert string into JSON object in jelly script?

vimal11592
Tera Expert

Hi,

I am trying to read the table data using GlideRecord in the jelly script.GlideRecord query gives us data as in string format but I have one field in the form that holds the data as follows:

[{"x":"y"},{"a":"b"}]

<g:evaluate var="jvar_gr" object="true">

var gr = new GlideRecord("x_14768_catalyst_bot_library");

gr.addQuery("bot_id", "test_380ee1a0");

gr.query();

gr;

</g:evaluate>

<j:while test="${jvar_gr.next()}" >

<p>${jvar_gr.getValue('params')}</p>

</j:while>

jvar_gr.getValue('params') returns data as in string but I want to convert it into JSON and loop the data and read the value of x and a.

Can someone please help me to resolve the issue.

Complete Code:

<?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:ui_form>

  <g:evaluate var="jvar_gr" object="true">

var gr = new GlideRecord("x_14768_catalyst_bot_library");

gr.addQuery("bot_id", "test_380ee1a0");

gr.query();

gr;

  </g:evaluate>

<j:while test="${jvar_gr.next()}" >

<p>${jvar_gr.getValue('params')}</p>

<j:forEach var="jvar_param" items="${jvar_gr.getValue('params')}">

------ READ THE VALUES   of A and X----

</j:forEach>

</j:while>

</g:ui_form>

</j:jelly>

1 ACCEPTED SOLUTION

Justin Abbott
Giga Guru

Could you use JSON.parse() to convert your string to a JSON object?


View solution in original post

6 REPLIES 6

Justin Abbott
Giga Guru

Could you use JSON.parse() to convert your string to a JSON object?


Hi Justin,



Thanks for the reply.I have tried doing the following :



<j:while test="${jvar_gr.next()}" >


<p>${jvar_gr.getValue('params')}</p>



<g:evaluate>


        var data = JSON.parse(${jvar_gr.getValue('params')})


</g:evaluate>



<j:forEach var="jvar_param" items="${data}">


------ READ THE VALUES   of A and X----


</j:forEach>


</j:while>



It didn't work.Can you please suggest if there are any changes in this.



Thanks,


Vimal


Try something like this, maybe.



<j:while test="${jvar_gr.next()}" >


      <p>${jvar_gr.getValue('params')}</p>


      <g:evaluate var="jvar_obj" object="true">


              var obj = JSON.parse(gr.getValue('params'));


              obj;


      </g:evaluate>


      ${jvar_obj.a}


      <br />


      ${jvar_obj.x}


</j:while>


Hi Justin



Thank you so much.I was struggling from couple of days to fix this.



I have tried the code and it worked.



code:



<j:while test="${jvar_gr.next()}" >


      <g:evaluate var="jvar_obj" object="true">


              var obj = JSON.parse(gr.getValue('params'));


              obj;


      </g:evaluate>


<j:forEach var="jvar_param" items="${obj}">


<g:evaluate jelly="true" object="true">


var botparam = jelly.jvar_param.label;


var botName = jelly.jvar_param.name;


</g:evaluate>


${botparam}


</j:forEach>


</j:while>



Thanks,


Vimal