You need to sign in to do that
Don't have an account?
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!!!
Hey can you show me your debug log for this..
Hi Abhi,
Here you go.
Thanks,
Sravan.
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"
}
]
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;}
}
}