Looping through all the fields from rest response

samadam
Kilo Sage

I have a rest response of a few hundred records. I need to iterate through all and set the fields. Is there a way to loop through the rest api response and get the field names and set them? my table field values match the rest response field names.

3 REPLIES 3

palanikumar
Mega Sage

Hi,

Please find the script to loop through all the keys and get the values. you can update it based on your requirement

var data = {"field1":"value1", "field2":"value2"}
for (var name in data) {
    gs.info(data[name])
}
Thank you,
Palani

Thank you, I tried it but getting only the first value

    var data = response.allData[i];
    for (var name in result){
        gs.info(result[name]);
}
only returns first field.

You are using variable named data. So update the loop with correct variable name
var data = response.allData[i];
    for (var name in data){
        gs.info(data[name]);
}
 
Thank you,
Palani