Help with JSON string.

Abhijit Das7
Tera Expert

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

1 ACCEPTED SOLUTION

@Abhijit Das7 

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.

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

View solution in original post

3 REPLIES 3

Weird
Mega Sage

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"

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.

@Abhijit Das7 

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.

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