Rest json response find correlation id

Training
Tera Contributor

Im creating incident to third party using POST REST. Then doing GET method to get number/sysid of that incident so I can set it as correlation id in my instance.

 

Using GET I'm getting response -

Transactions[

..

.

body{

Incident 1 In encrypted base 64

}

 

body{

 

Incident 2 In encrypted base 64

 

}

So on

 

How can I iterate through this response array and then decode it to set correlation id?

 

Please help.

 

12 REPLIES 12

@Training 

will it look like this for 1 record

{
"status": "success",
"Message": "retrieved",
"responses": [
{
"properties": {
"activity": "insert",
"source": "qatest"
},
"headers": {
"id": "10000",
"created": "2022-01-10 01:10:10"
},
"body": "eyJDbGIIbnRUaWNrZXQioilxMmRYjc0ZDFiNjNhNTEwZmViZ3ZiaGY1eWhnY2IzNCIzNCIsk51bWJIcil6lklOQzExMjM0NTYiLCJTeXNJRCI6ImdoNmQ3Y2E3Nml2YjY1NzU2ZDM1Y2JiNzQzNmJjYjhhIiwiV29yayBOb3RIcyI6IiJ9"
}
]
}

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Yes

If multiple incidents in get response then starting from "properties" it will get repeated for next records.

 

I'm able to get status and message which are outside array in logs using gs.log(responseBody.status + responseBody.Message)

 

Stuck for values inside array , especially body

@Training 

update as this if you want to get id value

something like this

var jsonString = '{"status":"success","Message":"retrieved","responses":[{"properties":{"activity":"insert","source":"qatest"},"headers":{"id":"10000","created":"2022-01-10 01:10:10"},"body":"eyJDbGIIbnRUaWNrZXQioilxMmRYjc0ZDFiNjNhNTEwZmViZ3ZiaGY1eWhnY2IzNCIzNCIsk51bWJIcil6lklOQzExMjM0NTYiLCJTeXNJRCI6ImdoNmQ3Y2E3Nml2YjY1NzU2ZDM1Y2JiNzQzNmJjYjhhIiwiV29yayBOb3RIcyI6IiJ9"}]}';

var parsedData = JSON.parse(jsonString);

var id = parsedData.responses[0].headers.id;
gs.info(id);

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

If I'm getting multiple incidents as response to GET then do i need to try using array and iterate? Do you know how to do it

@Training 

correct if the array within responses will be repeated then do this

var jsonString = '{"status":"success","Message":"retrieved","responses":[{"properties":{"activity":"insert","source":"qatest"},"headers":{"id":"10000","created":"2022-01-10 01:10:10"},"body":"eyJDbGIIbnRUaWNrZXQioilxMmRYjc0ZDFiNjNhNTEwZmViZ3ZiaGY1eWhnY2IzNCIzNCIsk51bWJIcil6lklOQzExMjM0NTYiLCJTeXNJRCI6ImdoNmQ3Y2E3Nml2YjY1NzU2ZDM1Y2JiNzQzNmJjYjhhIiwiV29yayBOb3RIcyI6IiJ9"}]}';

var parsedData = JSON.parse(jsonString);

for(var i=0;i<parseData.responses.length;i++){

var id = parsedData.responses[i].headers.id;
gs.info(id);

}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader