Need help to pass values in a JSON object
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2019 10:14 PM
Hello All,
I am trying to dynamically pass few value (which are fields on the form) into the JSON object which is as below:
var obj = {"name": "value1", "image": "value2"};
The value1 and value2 are the field values on an incident and i have a requirement where i need to pass those values.
Ex: where value1 = current.short_description && value2 == current.description which i need to pass on to obj variable. I have tried many different ways but couldn't be successful. Can some one help me out on this?
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2019 04:56 PM
Use below
var strObj = new JSON();
strObj.description = current.description;
strObj.short_description = current.short_description;
Pass the strObj to script where you want to use. For more details about the JSON read the ServiceNow doc link.
https://developer.servicenow.com/app.do#!/api_doc?v=madrid&id=r_JSON-decode_S
-harsh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2019 05:23 PM
YOu can also try something below.
var json = new JSON();
var Obj = {
"short_description" : current.short_description,
"description" : current.description
};
var data = json.encode(Obj);
return data;

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2019 09:39 PM
this is trial example:
var str = ‘{“name":"George","lastname":"Washington”}’;
var obj = JSON.parse(str);
gs.info(‘The first name is ’ + obj.name);