Parsing rest api data

samadam
Kilo Sage

I am getting back data from external system from rest api, I am trying to parse it but could not get it to work.

{
"meta": {
"totalItems": 736,
"pagingOffset": "0",
"pagingLimit": "1000"
},
"allPerson": [
{
"Id": 332,
"RCId": "000001",
"Type": 1,

}, 

{
"Id": 333,
"RCId": "000003",
"Type": 3,

}, 

]

I tried to parse using:

 var responseBody = response.getBody();
 var response1= JSON.parse(responseBody);
 for(i=0;i<2;i++) {
        // process json object
        gs.info('response1[i].Id);
  }
 
I cannot see the info. Is this the right way to do it?
3 REPLIES 3

Sandeep Rajput
Tera Patron
Tera Patron

@samadam Please update your code as follows and see if it works for you.

 

 var responseBody = response.getBody();
 var response1= JSON.parse(responseBody);
 for(i=0;i<2;i++) {
        // process json object
        gs.info(response1.allPerson[i].Id);
  }

Hope this helps.

That worked.

If I have a lot of fields is there a way to access all the field names and then set those values without manually specifying each field?

@samadam Glad to know it worked for you. Please mark the response an accepted solution and helpful.

 

Regarding your question related to field names, you need to set each one of them there are ways to extract the key value pairs from an object. However, they still need to be associated with the field names on form which needs to be done manually.