How to read key-value as a map or pair from a JSON

Shreyoshi1
Giga Contributor

Hi,

I have a JSON stored in a column of a table . I want to read it in script task of Virtual Agent Topic. I am doing the below but unable to get the keys or value, can anyone help with this please?

var obj = new global.JSON().encode(reqTemp); //reqTemp is the field read from the GlideRecord
for ( var key in obj)
{
     gs.info(" key is : " + key + " and value for key is " + obj[key]);
}
 
It just prints replaceAll as the key
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Try this alternative way

you need to use decode in order to convert that json string as object

var obj = new global.JSON().decode(reqTemp.toString());

for ( var key in obj)
{
     gs.info(" key is : " + key + " and value for key is " + obj[key]);
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

please try this

var obj = JSON.parse(reqTemp.toString());

for(var key in obj){

     gs.info(" key is : " + key + " and value for key is " + obj[key]);

}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Try this alternative way

you need to use decode in order to convert that json string as object

var obj = new global.JSON().decode(reqTemp.toString());

for ( var key in obj)
{
     gs.info(" key is : " + key + " and value for key is " + obj[key]);
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thank you, this worked.

You are welcome

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader