dynamic json script to store variable values
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2018 11:31 PM
Hi, I have a requirement where i need to store variable values as json object but it need to be dynamic, as in the variables are different for every record hence it should dynamically use the variable name as object name and the corresponding value. I am using this in the workflows so that i can get and store variable values for different catalog items.Can someone help me with the script?
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2018 11:37 PM
JSON object also supports bracket notation along with '.' access notation similar to array. So you can do something like
item = {}
item ["title"] = id;
item ["email"] = email;
The key value in the square bracket can be dynamic via your script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-06-2018 05:19 AM
Hi,
actually the same workflow would be used for different catalog items , so the script need to store variable values for whichever item orderd as json and input those values as string in incident task description. hence the script needs to be generic and used for multiple diffrernt variables.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-06-2018 11:51 PM
The key name inside square brackets can be dynamic. You just need to script it the way you want.
Sample:
var sampleKey = 'title';
item = {} item [sampleKey
] = id;

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2018 11:53 PM
Hi
you can do something like this :-
var temp= {};
temp.code = 'code';//Will create key value pair
temp.category = 'cat';
return temp;//can use this wherever you call this script include using temp.code or temp.category.
Mark Correct if it helps.
Warm Regards,
Omkar Mone
www.dxsherpa.com