Rest json response find correlation id
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2023 01:51 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2023 03:27 AM
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"
}
]
}
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2023 03:32 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2023 03:45 AM
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);
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2023 03:57 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2023 04:10 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader