Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Abhijit Das7
Tera Expert

Hi @Amit Verma 

 

Can you please also help me with this. I am little bit stuck with it.

 

Thanks in advance.

AttilaVarga
Kilo Sage
Kilo Sage

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.

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

@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';
}
...