GlideStringUtil.base64Decode returning an JSON object rather String
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā12-04-2024 11:56 AM
Hi All,
Thanks in advance for assisting me in resolving the issue.
I am trying to convert an encoded base64 to JSON object and read content.
Ex: encoded base64 message as below when I decode and JSON parse throwing an error
var inputMessage = "eyJndWlkIjoiMjhmZDdiYmQtM2U2Ny00MDMwLTlhN2YtN2YzMzk1Zjc3Nzg2IiwiYWN0aW9uIjoidXBkYXRlIiwiZmllbGRzIjoiZW1haWwsbGFzdF9uYW1lIn0=";
//inputMessage = inputMessage.pathParams.obj.toString();
var decodedString = GlideStringUtil.base64Decode(inputMessage); // this is returning typeof --object
var responseData = JSON.parse(decodedString);//I am getting error as unauthorisedtoken: u
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā12-04-2024 05:59 PM
That code actually works, but only in global scope. GlideStringUtil.base64Decode is not available in scopes.
Because of that running
var decodedString = GlideStringUtil.base64Decode(inputMessage);
sets decodedString to undefined, which is turned into string 'undefined', hence the u in the error message.
In scopes one should use gs.base64Decode(). Which on the other hand - JFYI - is not available in global.
Thus if I execute code
(
function () {
var inputMessage = "eyJndWlkIjoiMjhmZDdiYmQtM2U2Ny00MDMwLTlhN2YtN2YzMzk1Zjc3Nzg2IiwiYWN0aW9uIjoidXBkYXRlIiwiZmllbGRzIjoiZW1haWwsbGFzdF9uYW1lIn0=";
var decodedString = gs.base64Decode(inputMessage);
var responseData = JSON.parse(decodedString);
gs.debug(JSON.stringify(responseData, null, '\t'));
}
)();
in Scripts - Background, while selecting a scope, it runs and prints:
Script execution history and recovery available here
x_*****_dd: {
"guid": "28fd7bbd-3e67-4030-9a7f-7f3395f77786",
"action": "update",
"fields": "email,last_name"
} (sys.scripts extended logging)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā12-04-2024 07:55 PM
Hello @MadavapeddiK
Refer this script:
var inputMessage = "eyJndWlkIjoiMjhmZDdiYmQtM2U2Ny00MDMwLTlhN2YtN2YzMzk1Zjc3Nzg2IiwiYWN0aW9uIjoidXBkYXRlIiwiZmllbGRzIjoiZW1haWwsbGFzdF9uYW1lIn0=";
// Decode the base64 string
var decodedStream = GlideStringUtil.base64Decode(inputMessage);
// Convert the InputStream to a string using ServiceNow's method
var decodedString = new String(decodedStream);
var decodedJson = JSON.parse(decodedString);
// Access the fields from the JSON object
gs.print("Decoded JSON: " + decodedString);
gs.print("GUID: " + decodedJson.guid); // Accessing 'guid'
gs.print("Action: " + decodedJson.action); // Accessing 'action'
gs.print("Fields: " + decodedJson.fields); // Accessing 'fields'
Result:
Note: GlideStringUtil.base64Decode(inputMessage) returns a Java InputStream object, not a simple string. To work with the decoded data, you need to convert it to a string to access its content properly.
"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"
Thank You
Juhi Poddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā12-05-2024 02:13 AM
Actually the original code works just fine, but only in global scope.
Method stringify will automatically covert the input into a string.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā12-05-2024 01:22 AM
Thank you Juhi && Janos!! I am able to make it now. I have configured this Action where this data would be consumed from GooglePubSub.
Thanks,
Kartheek