function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sravan36sravan36 

JSON Deseriolization for Attachments not working

Hi All,

We are trying to do Seriolize and Deseriolize for list of attachments using JSON. But we came across some issue while deseriolizing attachments list the error is like Cannot deserialize instance of base64 from START_OBJECT value . Is it possible to Deseriolize list of attachments? Is there any problem with Blob data to deseriolize?

Below is the JSON code snippet.

list<Attachment> Attchmentslist = [select name,id,Body from Attachment where ParentId =: CId];
String JSonserlz = JSON.serialize(Attchmentslist);
list<Attachment> deserializedAttchmts = (list<Attachment>)JSON.deserialize(JsonSerlz, list<Attachment>.class);

Thanks in Advance!!!

Abhi_TripathiAbhi_Tripathi

Hey can you show me your debug log for this..

 

 

sravan36sravan36

Hi Abhi,

 

Here you go.

 

13:08:11.237 (1237073000)|SYSTEM_METHOD_EXIT|[134]|System.debug(ANY)
13:08:11.237 (1237092000)|METHOD_EXIT|[39]|01p90000003Sjit|JsonTestAPICheckcases.insertJsonrec(String)
13:08:11.237 (1237112000)|SYSTEM_METHOD_ENTRY|[40]|System.debug(ANY)
13:08:11.237 (1237128000)|USER_DEBUG|[40]|DEBUG|+++++JsonSerlz[{"attributes":{"type":"Attachment","url":"/services/data/v28.0/sobjects/Attachment/00P90000005Ti4ZEAS"},"Name":"New Text Document.txt","Body":{"length":36600,"inputStream":{},"maxToKeep":524288},"ParentId":"5009000000DD3spAAD","Id":"00P90000005Ti4ZEAS"},{"attributes":{"type":"Attachment","url":"/services/data/v28.0/sobjects/Attachment/00P90000005Ti82EAC"},"Name":"NET.doc","Body":{"length":31232,"inputStream":{},"maxToKeep":524288},"ParentId":"5009000000DD3spAAD","Id":"00P90000005Ti82EAC"},{"attributes":{"type":"Attachment","url":"/services/data/v28.0/sobjects/Attachment/00P90000005qcvwEAA"},"Name":"Batch Apex.docx","Body":{"length":24612,"inputStream":{},"maxToKeep":524288},"ParentId":"5009000000DD3spAAD","Id":"00P90000005qcvwEAA"},{"attributes":{"type":"Attachment","url":"/services/data/v28.0/sobjects/Attachment/00P90000005qex6EAA"},"Name":"Encryption and Hashing.docx","Body":{"length":20783,"inputStream":{},"maxToKeep":524288},"ParentId":"5009000000DD3spAAD","Id":"00P90000005qex6EAA"},{"attributes":{"type":"Attachment","url":"/services/data/v28.0/sobjects/Attachment/00P90000005qeP9EAI"},"Name":"Batch Apex.docx","Body":{"length":24612,"inputStream":{},"maxToKeep":524288},"ParentId":"5009000000DD3spAAD","Id":"00P90000005qeP9EAI"},{"attributes":{"type":"Attachment","url":"/services/data/v28.0/sobjects/Attachment/00P90000005qd6uEAA"},"Name":"Copy of Batch Apex.docx","Body":{"length":24612,"inputStream":{},"maxToKeep":524288},"ParentId":"5009000000DD3spAAD","Id":"00P90000005qd6uEAA"},{"attributes":{"type":"Attachment","url":"/services/data/v28.0/sobjects/Attachment/00P90000005qQgKEAU"},"Name":"Batch Apex.docx","Body":{"length":24612,"inputStream":{},"maxToKeep":524288},"ParentId":"5009000000Dso21AAB","Id":"00P90000005qQgKEAU"}]
13:08:11.237 (1237148000)|SYSTEM_METHOD_EXIT|[40]|System.debug(ANY)
13:08:11.237 (1237271000)|SYSTEM_METHOD_ENTRY|[41]|system.JSON.deserialize(String, system.Type)
13:08:11.239 (1239653000)|SYSTEM_METHOD_EXIT|[41]|system.JSON.deserialize(String, system.Type)
13:08:11.239 (1239746000)|FATAL_ERROR|System.JSONException: Cannot deserialize instance of base64 from START_OBJECT value { at [line:1, column:136]

(System Code)
Class.CheckcaseTest1.checkcase1: line 41, column 1
13:08:11.239 (1239773000)|CODE_UNIT_FINISHED|CheckcaseTest1 invoke(checkcase1)

 

Thanks,

Sravan.

Abhi_TripathiAbhi_Tripathi

Hey sravan, 

 You cannot easily dererialize this because it is having a pattern so , here you need to create a wrapper class for it, then bind that wrapper class when you are deseriallizing

 it will be easy to create a wrapper class when you understand the pattern

 

Here is the pattern from your debug logs,

 

{
"type":"Attachment",
"url":"/services/data/v28.0/sobjects/Attachment/00P90000005Ti4ZEAS"
},
"Name":"New Text Document.txt",
"Body":
{
"length":36600,
"inputStream":{},
"maxToKeep":524288
},
"ParentId":"5009000000DD3spAAD",
"Id":"00P90000005Ti4ZEAS"
},
{
"attributes":
{
"type":"Attachment",
"url":"/services/data/v28.0/sobjects/Attachment/00P90000005Ti82EAC"
},
"Name":"NET.doc",
"Body":
{
"length":31232,
"inputStream":{},
"maxToKeep":524288
},
"ParentId":"5009000000DD3spAAD",
"Id":"00P90000005Ti82EAC"
},{
"attributes":
{
"type":"Attachment",
"url":"/services/data/v28.0/sobjects/Attachment/00P90000005qcvwEAA"
},
"Name":"Batch Apex.docx",
"Body":
{
"length":24612,"inputStream":{},
"maxToKeep":524288
},
"ParentId":"5009000000DD3spAAD",
"Id":"00P90000005qcvwEAA"
},{
"attributes":
{
"type":"Attachment",
"url":"/services/data/v28.0/sobjects/Attachment/00P90000005qex6EAA"
},
"Name":"Encryption and Hashing.docx",
"Body":
{
"length":20783,
"inputStream":{},
"maxToKeep":524288
},
"ParentId":"5009000000DD3spAAD",
"Id":"00P90000005qex6EAA"
},{
"attributes":
{
"type":"Attachment",
"url":"/services/data/v28.0/sobjects/Attachment/00P90000005qeP9EAI"
},
"Name":"Batch Apex.docx",
"Body":
{
"length":24612,
"inputStream":{},
"maxToKeep":524288
},
"ParentId":"5009000000DD3spAAD",
"Id":"00P90000005qeP9EAI"
},{
"attributes":
{
"type":"Attachment",
"url":"/services/data/v28.0/sobjects/Attachment/00P90000005qd6uEAA"
},
"Name":"Copy of Batch Apex.docx",
"Body":
{
"length":24612,
"inputStream":{},
"maxToKeep":524288
},
"ParentId":"5009000000DD3spAAD",
"Id":"00P90000005qd6uEAA"
},{
"attributes":
{
"type":"Attachment",
"url":"/services/data/v28.0/sobjects/Attachment/00P90000005qQgKEAU"
},
"Name":"Batch Apex.docx",
"Body":
{
"length":24612,
"inputStream":{},
"maxToKeep":524288
},
"ParentId":"5009000000Dso21AAB",
"Id":"00P90000005qQgKEAU"
}
]

Abhi_TripathiAbhi_Tripathi

list<AttachementDeserialize> deserializedAttchmts = (list<AttachementDeserialize>)JSON.deserialize(JsonSerlz, list<AttachementDeserialize>.class);

 

Save this wrapper and then check debug of the above line.

Here is your wrapper class

 

public with sharing class AttachementDeserialize {

public Attributes attributes;
public Name Name;
public Body Body;
public ParentId ParentId;
public Id Id;

//inner class for holding variables of Attributes deserialize
public class Attributes {

public String type{get; set;}
public String url {get; set;}
}

//inner class for variables of holding Body deserialize
public class Body {

public String length {get; set;}
public String inputStream {get; set;}
public String maxToKeep {get; set;}
}
}