- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2024 10:52 PM - edited 03-08-2024 01:12 AM
Hi Everyone,
I have a JSON data, from which I have to extract Session ID from that JSON string:
JSON:
{"event":"Instruct: Session Start \n\n Session Start Time = Wed Mar 06 2024 14:03:56 GMT+0000 (Coordinated Universal Time) \n\n Experience = Last time snow 345678 \n\n
Experience ID = SRIndtDrsgfM4C97SGYM \n\n Session ID = 0efacb64-2135-43db-907a-d6a1affa12dd"}
My requirement is to get this 0efacb64-2135-43db-907a-d6a1affa12dd from the string.
cc: @Ankur Bawiskar
Thanks in advance
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2024 01:55 AM
enhancing script shared by other member
this worked for me
var obj = {"object_name":"wm_task","object_id":"8f2caf0097dc4650c0ebd404a253af65","fqdn":"dev21118.service-now.com","refId":"WOT0010011","Session":{"event":"Instruct: Session Start \n\n Session Start Time = Thu Mar 07 2024 07:02:45 GMT+0000 (Coordinated Universal Time) \n\n Experience = Testing instruct exp \n\n Experience ID = HNYsesDsGGfOtJRDkw6u \n\n Session ID = d69df864-e2e2-4c38-82ad-7bc62b721d84"}};
// Extract Session ID using regular expression
var sessionIdRegex = /Session ID = ([\w-]+)/;
var match = obj.Session.event.match(sessionIdRegex);
if (match && match.length > 1) {
var sessionID = match[1];
gs.info("Found session id: "+ sessionID);
} else {
gs.info("Session ID was not found");
}
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2024 11:30 PM
Well, first of all there's an extra curly bracket at the end. Is this part of some other JSON object?
Anyway, if we are just looking at this event, you should be able to do something like this:
var obj = {"event": "Instruct: Session Start \n\n Session Start Time = Wed Mar 06 2024 14:03:56 GMT+0000 (Coordinated Universal Time) \n\n Experience = Last time snow 345678 \n\n Experience ID = SRIndtDrsgfM4C97SGYM \n\n Session ID = 0efacb64-2135-43db-907a-d6a1affa12dd"
};
// Extract Session ID using regular expression
var sessionIdRegex = /Session ID = ([\w-]+)/;
var match = obj.event.match(sessionIdRegex);
if (match && match.length > 1) {
var sessionID = match[1];
gs.info("Found session id: "+ sessionID);
} else {
gs.info("Session ID was not found");
}
If you receive the JSON as a string, then you need to use JSON.parse(your_string) first to get it as an object.
Also the regex could break if the content is modified in any way, so it must always be "Session ID = your_id"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2024 11:40 PM - edited 03-08-2024 01:11 AM
Hi @Weird
Yes it is part of another JSON object. I tried your method but it is not working. It is giving 500 internal Error. Can you suggest something using indexOf.
This is complete JSON:
{"object_name":"wm_task",
"object_id":"8f2caf0097dc4650c0ebd404a253af65",
"fqdn":"dev21118.service-now.com",
"refId":"WOT0010011",
"Session":{"event":"Instruct: Session Start \n\n Session Start Time = Thu Mar 07 2024 07:02:45 GMT+0000 (Coordinated Universal Time) \n\n Experience = Testing instruct exp \n\n Experience ID = HNYsesDsGGfOtJRDkw6u \n\n Session ID = d69df864-e2e2-4c38-82ad-7bc62b721d84"}}
Thanks in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2024 01:55 AM
enhancing script shared by other member
this worked for me
var obj = {"object_name":"wm_task","object_id":"8f2caf0097dc4650c0ebd404a253af65","fqdn":"dev21118.service-now.com","refId":"WOT0010011","Session":{"event":"Instruct: Session Start \n\n Session Start Time = Thu Mar 07 2024 07:02:45 GMT+0000 (Coordinated Universal Time) \n\n Experience = Testing instruct exp \n\n Experience ID = HNYsesDsGGfOtJRDkw6u \n\n Session ID = d69df864-e2e2-4c38-82ad-7bc62b721d84"}};
// Extract Session ID using regular expression
var sessionIdRegex = /Session ID = ([\w-]+)/;
var match = obj.Session.event.match(sessionIdRegex);
if (match && match.length > 1) {
var sessionID = match[1];
gs.info("Found session id: "+ sessionID);
} else {
gs.info("Session ID was not found");
}
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