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

Glad to help, Vimal!


jamesmalvi
Kilo Contributor

Hope this article helps.

https://codeblogmoney.com/convert-string-to-json-using-javascript/

 

It converts JSON string to javascript object.

 

Thanks,

James