How to parse Encoded Json Response in Rest API

Ashok32
Tera Contributor

Hi All,

 

I have a requirement like When I create an incident then it will hits the third party and if they update something like comments and add attachments and state updates then we need to get those updates and update it into the corresponding incident.

For this they send response as incident number and message ID and action.

For getting those updates I had created a Get method and I can able to get those updates as a Response successfully.

 

But, When I parse that response it can't work because they are sending attachments as encoded format so that I can't able to parse the other responses.

Script I used for parsing is 

var r = new sn_ws.RESTMessageV2('X', 'Get All Updates');
var response = r.execute();
var responseBody = response.getBody();
var obj = JSON.parse(responseBody);
 
for (var i = 0; i < obj.result.length; i++) {
    var r1 = new sn_ws.RESTMessageV2('x', 'Get single update-addcomment');
    r1.setStringParameterNoEscape('inc', obj.result[i].id.toString());
    r1.setStringParameterNoEscape('msg_id', obj.result[i].messageId.toString());
    r1.setStringParameterNoEscape('action', obj.result[i].action.toString());
    var response1 = r1.execute();
    var responseBody1 = response1.getBody();
  }

 

 

Please help me how to parse when the response contains these kind of things.

 

 

 

Thanks in advance.

2 REPLIES 2

Amit Gujarathi
Giga Sage
Giga Sage

HI @Ashok32 ,
I trust you are doing great.
Here's an example of how you might adjust your script:

var r = new sn_ws.RESTMessageV2('X', 'Get All Updates');
var response = r.execute();
var responseBody = response.getBody();

// Assuming the encoded attachment is separated in the response
var parsedResponse = separateAndDecodeAttachment(responseBody);

var obj = JSON.parse(parsedResponse.nonEncodedPart);

for (var i = 0; i < obj.result.length; i++) {
    // Handle non-attachment parts
    var r1 = new sn_ws.RESTMessageV2('x', 'Get single update-addcomment');
    r1.setStringParameterNoEscape('inc', obj.result[i].id.toString());
    r1.setStringParameterNoEscape('msg_id', obj.result[i].messageId.toString());
    r1.setStringParameterNoEscape('action', obj.result[i].action.toString());
    var response1 = r1.execute();
    var responseBody1 = response1.getBody();
    // Process response1 as needed

    // Handle attachments
    if(parsedResponse.encodedAttachment) {
        // Decode the attachment
        var decodedAttachment = decodeAttachment(parsedResponse.encodedAttachment);
        // Add the attachment to the incident
        addAttachmentToIncident(obj.result[i].id, decodedAttachment);
    }
}

function separateAndDecodeAttachment(responseBody) {
    // Logic to separate the encoded attachment and the rest of the response
    // Return an object with 'encodedAttachment' and 'nonEncodedPart'
}

function decodeAttachment(encodedAttachment) {
    // Logic to decode the attachment
    // Return the decoded attachment
}

function addAttachmentToIncident(incidentId, attachment) {
    // Logic to add the attachment to the specified incident
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Hi @Amit Gujarathi ,

 

Thanks for your response.

Can you please provide the script to separate the encoded and non encoded part from the response.