How to parse the rest message response and populate it on custom table?

rajkrish
Tera Contributor

Hi,

        I'm able to get back Json inputs from REST message

[ {"SiteName":"SC","LabName":"SVC","UnitNumber":"M5835JC800490","MaterialLotNumber":"L542C251","ProductName":"BROADWELL EP 24C","PartTypeName":"MF 8PTSFV A B","FullLocationName":"SHIP: CR3-1-C35 Mega Lab Inventory Control","QDFNumber":"","LastVPONumber":"","LastStepName":"","LastBinName":"","MirNumber":63362,"MirRequesterWWID":"10519277","MirRecipientWWID":"10552840"},

{"SiteName":"SC","LabName":"SVC","UnitNumber":"M5424BB700234","MaterialLotNumber":"L541C677","ProductName":"BROADWELL EP 24C","PartTypeName":"MF 8PTSFV A B","FullLocationName":"SHIP: CR3-1-C35 Mega Lab Inventory Control","QDFNumber":"","LastVPONumber":"","LastStepName":"","LastBinName":"","MirNumber":63362,"MirRequesterWWID":"10519277","MirRecipientWWID":"10552840"}]

I need to insert   these records into a custom table(lets say u_demo table). Kindly help me with a proper coding.

Thanks,

Rajkumar.

1 ACCEPTED SOLUTION

Hi Rajkumar,



      There is small changes in code which i had missed earlier.



      Try this latest modified code. Hope it will work!



      var r = new sn_ws.RESTMessageV2('REST_name_xxxx', 'get'); // in my case, we are using Get method


      var response = r.execute();


      var responseBody = response.getBody(); //fetches JSON response


      var response= JSON.parse(responseBody);



      for(i=0;i<response.length;i++)


        {


                            var gr = new GlideRecord('u_labname');


                          gr.initialize();


                        gr.u_fieldname = response[i].SiteName;


                          ....................


                        ...................


                      gr.insert();


        }



Thanks,


Kaka Nayeem




PS: Hit like, Helpful or Correct depending on the impact of the response


View solution in original post

10 REPLIES 10

Deepa Srivastav
Kilo Sage

Access those values using request.body.data.variablename. Check below blog for more info.


Scripted Rest APIs - Part 1



Mark Correct if it solved your issue or hit Like and Helpful if you find my response worthy.


Mrudula6
Mega Guru

Hi Rajkumar,



In your server code, you can fetch the JSON response using below script.



var r = new sn_ws.RESTMessageV2('REST_name_xxxx', 'get'); // in my case, we are using Get method


var response = r.execute();


var responseBody = response.getBody();//fetches JSON response


var abc= JSON.parse(responseBody);


current.xx_fieldname= abc.SiteName; // SiteName being the variable from JSON


gs.addInfoMessage("Sitename is   "+abc.SiteName);


current.update();


Hi Mrudula,




(function executeRule(current, previous /*null when async*/) {


try {


var r = new sn_ws.RESTMessageV2('XXX', 'get');




//override authentication profile


//authentication type ='basic'/ 'oauth2'


//r.setAuthentication(authentication type, profile name);




var response = r.execute();


var responseBody = response.getBody();


  //var r1= response.getRequestBody();


var httpStatus = response.getStatusCode();



    gs.addInfoMessage(response);


    gs.addInfoMessage(responseBody);


    //gs.addInfoMessage(r1);


    gs.addInfoMessage(httpStatus);


}


catch(ex) {


var message = ex.getMessage();


}




})(current, previous);



Using this I'm able to get JSON response back


[{"SiteName":"SC","LabName":"SVC","UnitNumber":"M5835JC800490","MaterialLotNumber":"L542C251","ProductName":"BROADWELL EP 24C","PartTypeName":"MF 8PTSFV A B","FullLocationName":"SHIP: CR3-1-C35 Mega Lab Inventory Control","QDFNumber":"","LastVPONumber":"","LastStepName":"","LastBinName":"","MirNumber":63362,"MirRequesterWWID":"10519277","MirRecipientWWID":"10552840"},{"SiteName":"SC","LabName":"SVC","UnitNumber":"M5424BB700234","MaterialLotNumber":"L541C677","ProductName":"BROADWELL EP 24C","PartTypeName":"MF 8PTSFV A B","FullLocationName":"SHIP: CR3-1-C35 Mega Lab Inventory Control","QDFNumber":"","LastVPONumber":"","LastStepName":"","LastBinName":"","MirNumber":63362,"MirRequesterWWID":"10519277","MirRecipientWWID":"10552840"},].


Now I need to insert these records into a custom table(u_demo) ,lets say the table columns are created for SiteName (u_sitename), LabName(u_labname) and same for other fields > Kindly help me where to modify the code to pass these inputs to the custom table.



Thanks,


Rajkumar


Hi Rajkumar,


Try this, i hope it will work!


var r = new sn_ws.RESTMessageV2('REST_name_xxxx', 'get'); // in my case, we are using Get method


var response = r.execute();


var responseBody = response.getBody(); //fetches JSON response


var response= JSON.parse(responseBody);



for(i=0;i<response.length;i++)


{


      var gr = new GlideRecord('u_labname');


      gr.initialize();


      gr.u_fieldname = response.SiteName;


      ....................


      ...................


      gr.insert();


}



Thanks,


Kaka Nayeem




PS: Hit like, Helpful or Correct depending on the impact of the response