Following code is giving me common.apex.runtime.impl.ExecutionException
String jsonText = '{"Data": {"attributes": {"type": "Contact"},"Id": "0036100000JUXKkAAP","Description": "","LastName": "Testing"}}';
Map<String, Object> cObjMap = (Map<String, Object>) JSON.deserializeUntyped(jsonText); String cObjJson = JSON.serialize(cObjMap.get('Data')); SObject customObject = (SObject)JSON.deserialize(cObjJson, SObject.class);
The problem is in fact that Map<String,Object> gets re-ordered during serialize/deserialize and "attributes" node is moved to last position.
That causes deserialize to SObject to fail as it requires "attributes" to be in first position, just like in jsonText example.
I know that defining separate class that would map all key/value pairs from JSON and deserializing to it would work, but I don't have exact structure as keys might vary.
Do you have any idea how to accomplish this?
Thank You!
cObjMap.get('Data')
? If you try and cast it to sObject you get the error: Invalid conversion from runtime type Map<String,ANY> to SObject. So at best it isMap<String, Object>
. Yes you can serialize it, but I don't think you can deserialize it as an sObject class instance. Nor can you deserialize aMap<String, Object>
as Object isn't supported.