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
Priya Raj 22Priya Raj 22 

serialize the results of a SOQL query into JSON

how to serialize the results of a SOQL query into JSON into a apex class.

SOQL query : SELECT Id, AccountId  form  Opportunity WHERE Id = :parentOppId
Shubham Raj 29Shubham Raj 29

Hi @Priya Raj 22,

Try this-

opportunity opp = [SELECT Id, AccountId  from  Opportunity WHERE Id =: parentOppId];
String jsonBody = JSON.serialize(opp);
system.debug('jsonBody'+jsonBody);
 

Let me know if this was helpful. If it does, please mark it as Best Answer to help others too.

Thanks,
Shubham Raj

Suraj Tripathi 47Suraj Tripathi 47
Hi Priya,

Opportunity Opportunityobj = new Opportunity();

Opportunityobj =  [SELECT Id, AccountId  form  Opportunity WHERE Id = :parentOppId];

String jsonString  = JSON.serialize(Opportunityobj);

Try the above code snippet

If it helps Please Mark as the best answer

Thanks