- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2024 05:36 AM
Hi Everyone,
I am creating Scripted REST API to get data from JSON to Incident work notes.
JSON payload :
{
"data": [
{
"type" : "line",
"Name" : "Ravi"
},
{
"type":"multiline",
"Address" : "XYZ"
},
{
"type":"radioButton",
"Gender":"Male",
}
]
Can anyone guide me how can I use this array to post this data to work notes in incident. In each object of array has two fields :
1. Type
2. Second field can be anything Name , Address , Gender , email, phone etc.
I am facing difficult in using second field which is dynamic.
Thanks in advance.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2024 06:43 AM
Hi,
Below is the script to get dynamic keys from your JSON.
var obj = { "data": [
{
"type" : "line",
"Name" : "Ravi"
},
{
"type":"multiline",
"Address" : "XYZ"
},
{
"type":"radioButton",
"Gender":"Male",
}
]};
for(var i=0;i<obj.data.length;i++){
var obj1 = obj.data[i];
for(key in obj1){
gs.info("key is "+key+" and value is "+obj1[key]);
}
}
you can use above logic to extract the values as per your requirement
Thanks
Anil Lande
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2024 10:11 PM
Try as below :
var Submission = parsedData.submission;
var WorkNotes = '';
for (var t = 0; t < Submission.length; t++) {
var obj1 = Submission[t];
for (key in obj1) {
str = key + ' : ' + obj1[key] + '\n';
gs.info(str);
WorkNotes+= str;
}
}
Please mark this response as correct and helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2024 06:10 AM
Hello @Abhijit Das7 ,
I am assuming the data is array, then the code should be something like this, Using above method, you can set the incident's work notes.
var data = [
{
"type" : "line",
"Name" : "Ravi"
},
{
"type":"multiline",
"Address" : "XYZ"
},
{
"type":"radioButton",
"Gender":"Male",
}
]
gs.info(data[0].type) // it will give you type of first object
gs.info(data[1].Address) // it will give you addressof first object
gs.info(data[2].Gender) // it will give you gender of first object
If this not works for you, Please share all the JSON content to me. So it will easy to understand the issue.
Kindly mark the answer ✔️Correct or Helpful ✔️If it addresses your concern.
Regards,
Siddhesh

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2024 06:43 AM
Hi,
Below is the script to get dynamic keys from your JSON.
var obj = { "data": [
{
"type" : "line",
"Name" : "Ravi"
},
{
"type":"multiline",
"Address" : "XYZ"
},
{
"type":"radioButton",
"Gender":"Male",
}
]};
for(var i=0;i<obj.data.length;i++){
var obj1 = obj.data[i];
for(key in obj1){
gs.info("key is "+key+" and value is "+obj1[key]);
}
}
you can use above logic to extract the values as per your requirement
Thanks
Anil Lande
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2024 05:46 AM - edited 02-04-2024 10:30 PM
Hi @Anil Lande
I tried your suggestion but it is giving only last data. It is only printing :
"Gender":"Male"
My code :
var Submission = parsedData.submission;
for (var t = 0; t < Submission.length; t++) {
var obj1 = Submission[t];
for (key in obj1) {
str = key + ' : ' + obj1[key] + '\n';
gs.info(str);
var WorkNotes = str;
}
}
But I can see all the data in gs.info ( all 6 ). But while printing in work notes it is showing only one (last one).
Rest data is not coming.
cc: @Ankur Bawiskar - Can you please guide me on this.
Thanks in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2024 09:14 PM
Can you tweak the line
var WorkNotes = str;
to
var WorkNotes+= str;
and try ?
Thanks & Regards
Amit Verma
Please mark this response as correct and helpful if it assisted you with your question.