+ Start a Discussion
Vidya BhandaryVidya Bhandary 

Multiple resultset into one JSON

I have created an APEX REST class that queries two different objects and returns multiple rows for both queries. Both queries return completely different values.

I want to get the results of both the resultsets into one JSON object which will be automatically parsed due to the REST call.

Is this possible ?


I tried to put both resultsets into a single <sObject> list but it gave me an error saying two different types of objects were being added into a single list.

Best Answer chosen by Vidya Bhandary
Ankit Gupta@ DeveloperAnkit Gupta@ Developer
Hi,

Try the bleow code snippet as reference:

---------------- Vf page ----------
<apex:page controller="createjson" >
{!jsonobj}

{!objlst}
</apex:page>

--------------- Apex class -----------------

public class createjson
{
public string jsonobj{get;set;}
public list<sObject >objlst{get;set;}
public createjson()
{
    list<contact>con=[select id,name from contact limit 1];
    jsonobj=json.serializePretty(con);
     list<account>acc=[select id,name from account limit 1];
     jsonobj=jsonobj+','+json.serializePretty(acc);
     objlst=new list<sObject >();
     objlst=(list<sObject >)json.deserialize(jsonobj,list<sObject >.class);
}

}

Regards
Ankit Gupta

All Answers

Ankit Gupta@ DeveloperAnkit Gupta@ Developer
Hi,

Try the bleow code snippet as reference:

---------------- Vf page ----------
<apex:page controller="createjson" >
{!jsonobj}

{!objlst}
</apex:page>

--------------- Apex class -----------------

public class createjson
{
public string jsonobj{get;set;}
public list<sObject >objlst{get;set;}
public createjson()
{
    list<contact>con=[select id,name from contact limit 1];
    jsonobj=json.serializePretty(con);
     list<account>acc=[select id,name from account limit 1];
     jsonobj=jsonobj+','+json.serializePretty(acc);
     objlst=new list<sObject >();
     objlst=(list<sObject >)json.deserialize(jsonobj,list<sObject >.class);
}

}

Regards
Ankit Gupta
This was selected as the best answer
Vidya BhandaryVidya Bhandary
Hi Ankit

Thank you for your reply.

I tried this way and although I am able to concatenate the resultsets into one - it is not deserializing it properly !!!
It shows only the attributes of the first result set after deserializing.

So

jsonobj = jsonobj + ',' + json.serializePretty(sr_result);
 
System.debug(jsonobj); ----> This shows all records but it is encoded like this [ {}, {}, {}, {} ],[ {} ]

But you solved the different objects problem; now there is no error on formation of the new sObject list.

Vidya BhandaryVidya Bhandary
Although the new list is not picking up the results of the 2nd query into itself.

objlst = new list<sObject>();
 
  objlst = (list<sObject>)json.deserialize(jsonobj,list<sObject>.class);
 
 
  System.debug(objlst);


Here I see the list does not have the 2nd results.
Vidya BhandaryVidya Bhandary
okay I got it ... I used replace method  and removed the substring "] [" from the resulting jsonobj and then passed it to the deserialize method. Worked !!!!

I am yet to test if given different set of variables like say 4 vars in result set 1 and 7 vars in result set 2 - it works or breaks but logically it should work !! :)

Tx