Help with JSON array

Abhijit Das7
Tera Expert

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.

 

2 ACCEPTED SOLUTIONS

Anil Lande
Kilo Patron

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 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

View solution in original post

@Abhijit Das7 

 

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.

View solution in original post

6 REPLIES 6

Hi @Amit Verma 

 

I tried your method but it is giving error. I cannot save Scripted REST API.

 

Thanks 

@Abhijit Das7 

 

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.