Help with JSON array inside array.

Abhijit Das7
Tera Expert

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:

 var Submission = parsedData.submission;
                    var WorkNotesS = '';
                    for (var t = 0; t < Submission.length; t++) {
                        var obj1 = Submission[t];

                        for (key in obj1) {

                            str = key + ' : ' + obj1[key] + '\n' ;
                            gs.info(str);

                            WorkNotesS+= str;

                           var WorkNotes WorkNotesS;

                        }
                    }
 
I have one array inside another in payload . If type is checkbox then I will have array with any name, in this case it is "Hobby" but it will be dynamic.
 
Outcome: Result is not coming as expected .
type : singleLine
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
 
Can anyone suggest something so that I can get values of the second array as well.
 
Thanks in advance.
1 ACCEPTED SOLUTION

@Abhijit Das7

 

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);

 

 

View solution in original post

8 REPLIES 8

Hi @AttilaVarga 

 

Last doubt I have is this that can I create space like this:

type: singleLine
Full Name: ahgdvashdv

type: multiLine
Address: x canmbcas

type: radioButton
Gender: Female

type: dropDown
Qualification: UG

type: checkBox
Hobby
Gardening: 1
Reading: 1
Playing: 1
Travelling: 0

type: rating
Rate Us: 4

type: thumbRating
How was your experience with us?: 1

 

Currently it is coming like this:

type : singleLine
Full Name : hgfhjg
type : multiLine
Address : hgfkjhbj
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

 

Thanks in advance

@Abhijit Das7

 

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);

 

 

@Abhijit Das7 

 

Can you try this :

 

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\n';
} else {
    str = key + ' : ' + obj1[key] + '\n\n';
}

Please mark this response as correct and helpful if it assisted you with your question.

piyushsain
Tera Guru
Tera Guru

@Abhijit Das7 

Use the below script in REST 

var Submission = parsedData.submission;
                    var WorkNotesS = '';
                    for (var t = 0; t < Submission.length; t++) {
                        var obj1 = Submission[t];

                        for (key in obj1) {
                            if(key=='Hobby'){
                                var Hobby = obj1[key];
                   
                    for (var t1 = 0; t1 < Hobby.length; t1++) {
                        var obj11 = Hobby[t1];

                        for (key1 in obj11) {
                            str = key1 + ' : ' + obj11[key1] + '\n' ;
                            gs.info(str);
                        }
                    }
                            }
                            else
                            str = key + ' : ' + obj1[key] + '\n' ;
                            gs.info(str);

                            WorkNotesS+= str;

                           var WorkNotes =  WorkNotesS;

                        }
                    }
If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,
Piyush Sain