Fix Script to read a json file attachment
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2024 09:42 AM
It's a simple thing,
There is a file in sys_attachment table of type application/json with following content:
{
"parent": "",
"made_sla": "true",
"upon_reject": "cancel",
"sys_updated_on": "2024-04-29 13:20:17",
"child_incidents": "0",
"task_effective_number": "INC0008001",
"approval_history": "",
"number": "INC0008001",
"sys_updated_by": "admin",
"sys_created_on": "2021-01-15 21:04:34",
"state": "1",
"sys_created_by": "admin",
"knowledge": "false",
....
}
I'm trying to read it, parse it and simply print the values.
Here is how I'm doing it:
var attachment = new GlideSysAttachment();
var agr = attachment.getAttachments('u_incident_dummy', '0811f7a183f10210fd277a65eeaad3a0'); //create attachment GlideRecord
while (agr.next()) { //for each attachment on the incident record
gs.print(agr.getValue('file_name')); //print file name of attachment
var content = attachment.getContent(agr);
//JSON parser
var parser = new JSONParser();
var parsedData = parser.parse(content);
for (var j in parsedData) {
gs.print("JSON Content: " + j + "=" + parsedData[j]);
}
}
Nothing shows up when I run it, except the file name, am I doing something wrong here?
1 REPLY 1

Community Alums
Not applicable
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2024 10:23 AM - edited 04-29-2024 10:26 AM
Hi @Mehant Kumar1 ,
I tried your problem in my PDI it works for me please refer below script
var gsa = new GlideSysAttachment();
var bytesInFile = gsa.getBytes('u_incident_dummy', '0811f7a183f10210fd277a65eeaad3a0');
var dataAsString = Packages.java.lang.String(bytesInFile);
dataAsString = String(dataAsString);
gs.print(dataAsString);
I added your given JSON to incident table and it gives the same output
Please mark my answer correct and helpful if this works for you
Thanks and Regards
Sarthak