- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2024 10:38 PM - edited 02-05-2024 12:32 AM
Hi Everyone,
I am writing Scripted Rest API to get data from JSON and paste that data into Incident work notes.
Payload:
{"submission": [
{
"type": "singleLine",
"Full Name": "hgfjhgjkjk"
},
{
"type": "multiLine",
"Address": "jgjkkln"
},
{
"type": "radioButton",
"Gender": "Male"
},
{
"type": "dropDown",
"Qualification": "PG"
},
{
"type": "checkBox",
"Hobby": [
{
"Gardening": "0"
},
{
"Reading": "0"
},
{
"Playing": "1"
},
{
"Travelling": "1"
}
]
},
{
"type": "rating",
"Rate Us": "4"
},
{
"type": "thumbRating",
"How was your experience with us?": "1"
}
]
}
Scripted Rest API:
Full Name : hjjkgjkhj
type : multiLine
Address : vjkbklj
type : radioButton
Gender : Male
type : dropDown
Qualification : PG
type : checkBox
Hobby : [object Object],[object Object],[object Object],[object Object] // Here it is getting problem.
type : rating
Rate Us : 4
type : thumbRating
How was your experience with us? : 1
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2024 12:43 AM
If you don't mind I modified the code a bit (There is no error handling in the code, if it is necessary you need to add some):
var parsedDataStr = parsedData.submission.map(function(currentItem) { // ES5 safe array process method
var currentElemStr;
for (key in currentItem) {
if (key === "type") {
currentElemStr = key + ': ' + currentItem[key] + '\n';
} else {
if (Array.isArray(currentItem[key])) {
var subArrayItems = currentItem[key].map(function(currentSubItem) {
return Object.keys(currentSubItem) + ' : ' + currentSubItem[Object.keys(currentSubItem)];
}).join('\n');
currentElemStr += key + '\n' + subArrayItems + '\n';
} else {
currentElemStr += key + ': ' + currentItem[key] + '\n';
}
}
}
return currentElemStr;
}).join('\n');
gs.info(parsedDataStr);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2024 11:07 PM
Hi @Amit Verma
Can you please also help me with this. I am little bit stuck with it.
Thanks in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2024 11:30 PM
Hi @Abhijit Das7,
If I understand correctly, you would like to see the 'real' content of subarray. You can try the following code (I modified your one):
var Submission = parsedData.submission;
var WorkNotesS = '';
for (var t = 0; t < Submission.length; t++) {
var obj1 = Submission[t];
for (key in obj1) {
var str;
if (Array.isArray(obj1[key])) {
str = obj1[key].map(function(currentItem) { // ES5 safe array process method
return Object.keys(currentItem) + ' : ' + currentItem[Object.keys(currentItem)];
}) + '\n';
} else {
str = key + ' : ' + obj1[key] + '\n';
}
gs.info(str);
WorkNotesS += str;
var WorkNotes = WorkNotesS;
}
}
I hope it helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2024 11:52 PM
Hi @AttilaVarga
I am getting like this :
type : checkBox
Gardening : 0,Reading : 0,Playing : 1,Travelling : 1
Can I get like this:
type: checkBox
Hobby // this is missing in above code.
Gardening: 1
Reading: 1
Playing: 1
Travelling: 0
With your suggestion "Hobby" is not coming and all the data in coming in single line. I need them different lines.
Thanks in advance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2024 12:14 AM
@Abhijit Das7, try this one:
...
if (Array.isArray(obj1[key])) {
var subArrayItems = obj1[key].map(function(currentItem) { // ES5 safe array process method
return Object.keys(currentItem) + ' : ' + currentItem[Object.keys(currentItem)] + '\n';
}).join('\n');
str = key + ' :\n' + subArrayItems + '\n';
} else {
str = key + ' : ' + obj1[key] + '\n';
}
...