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
Devashree Tidke 23Devashree Tidke 23 

Can I deserialize JSON string if I dont know the type ?

I am trying to store integration response in JSON format in a custom object (CustomResponseObject__c) to improve the performance. When I retrieve the JSON string after deserialization it can be instance of List<customIntegrationWrapperClass> or CustomIntegrationErrorLog__c Object. How do I deserialize the JSON string not knowing the object type ? 
I am trying this :
List<Object> storedResponse = (List<Object>)JSON.deserializeUntyped(listCustomResponseObject[0].JSON__c);
if(storedResponse[0] instanceOf customIntegrationWrapperClass){
    System.debug('Integration Success !!!'); //This line is never printed :(
}

The following code works :
List<Object> lstObject = (List<customIntegrationWrapperClass>)JSON.deserialize(listCustomResponseObject[0].JSON__c, List<customIntegrationWrapperClass>.class);

Any pointers ?
Raj VakatiRaj Vakati
i think this is how it will be
 
List<CustomResponseObject__c> storedResponse = (List<CustomResponseObject__c>)JSON.deserializeUntyped(listCustomResponseObject[0].JSON__c);
if(storedResponse[0] instanceOf CustomResponseObject__c){
    System.debug('Integration Success !!!'); //This line is never printed :(
}

 
Devashree Tidke 23Devashree Tidke 23
Thanks but that won't work. Actually my problem is I don't know if the JSON string is going to be of List<customIntegrationWrapperClass> type always, if I had known the type, JSON.deserialize would have directly worked for my requirement.