- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-10-2020 05:55 AM
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?
Solved! Go to Solution.
- Labels:
-
Agent Workspace
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-10-2020 06:12 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-10-2020 06:17 AM
JSON().encode is depreciated.
The encode() and decode() methods are deprecated. Use the JavaScript JSON object instead.
https://developer.servicenow.com/dev.do#!/reference/api/orlando/server_legacy/c_JSONAPI
Do you have a column of type String?
If so, you'll need to parse().
var jsonString = '{"key1" : "value1","key2" : "value2","key3" : "value3"}';
var obj = JSON.parse(jsonString);
for ( var key in obj)
{
gs.info(" key is : " + key + " and value for key is " + obj[key]);
}
Result:
*** Script: key is : key1 and value for key is value1
*** Script: key is : key2 and value for key is value2
*** Script: key is : key3 and value for key is value3

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-10-2020 06:18 AM
Seems like Ankur beat me to the posting the reply. Anyways, encode() and decode() is depreciated and shouldn't be used.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-10-2020 06:22 AM
Somehow it still shows in developer site; seems then time to update that
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader