How to convert String field into a JSON format

Sri56
Tera Contributor

Hi All,

I have a string field called 'Response Body' on my incident table.

where i am printing response from one of the API.

Now the response is in string.

Can someone, please help me how to convert this string into a json format.

Please find the attached screenshot for reference.

 

Thanks,

Sri

14 REPLIES 14

 

What are you trying to do here? You're printing the response body into a string field and expecting what? Are you just trying to print it in a pretty format with line breaks and indentations etc?

Sri56
Tera Contributor

Hi David,

 

Yes, we actually need a response to be in pretty format with line breaks and indentations.

 

As Below:

{
   "messages": "ok",
   "result": [
      {
         "state": "Closed",
         "updated": "2019-08-08 11:36:43",
         "description": " STATE ALARM OF PARAMETER PROCPPCOUNTCHECK TRIGGERED ON PROCPRES.AEM_ENDECA <=2<=",
         "title": "AEMPROD ENDECA SERVER ENDECA PROCESS DOWN ON HOST: SPKVPLINMDEX01",
         "assigned_to": "Ankita Talukdar",
         "configuration_item": "QVC Webshop - DE",
         "created": "2019-08-02 21:32:25",
         "opened_by": "ESM Create",
         "incident": "INC0079495",
         "score": 0,
         "assignment_group": "Digital - Site Operations",
         "resolution": "This has been resolved.This was due to service recycle which was done as part of DE TSV search issue on this morning.",
         "closed_by": "Ankita Talukdar"
      },

Thanks,

Sri

ok use this then:

var responseBody = response.getBody();
var requestBody = r.getRequestBody();
gs.log('Request Body for QVC Integration request: '+requestBody);
gs.log('Response Body for QVC Integration request: '+responseBody);
var json = JSON.parse(responseBody);
current.u_response_body = JSON.stringify(json, null, 2);

Not sure if you have to parse it before stringifying it but you can test that out yourself.

Sri56
Tera Contributor

Hi David,

We got an expected output.Thanks a lot:)

 

Thanks,

Sri

Cool, glad it's working. Please mark my answer correct to close the thread down.