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
RajanRajan 

How to convert response in jSON format

Hi Friends,
My class is as below and I need to return the response in JSOn format. My class is returning Accoun t and related contacts. Can any one please help me out?
My class:
*****************
public with sharing class AccountContactList{
public List<account> accountlist{get;set;}
public set<Id> accountids= new set<Id>();
public  List<contact> contactlist{get;set;}
//public string eventid{get;set;}
public AccountContactList(){
   accountlist= [select Id,Name FROM Account];
   for(Account a : accountlist){
       accountids.add(a.Id);
         }
}
public void getcontactdetails(){
contactlist= [select Id,AccountId,Name From contact WHERE AccountId In:accountids];
 system.debug('ContactList:' +contactlist);
}
}


 
VamsiVamsi
Hi,

You can get the JSON formt using JSON.serialize method.

Please go through the following links

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_Json.htm

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_JsonGenerator.htm

Please mark as best answer, if the above helps ....!!!!!
V TV T
This blog helped me to solve my requierment same as yours.
SOQL result in JSON (https://salesforcetrail.blogspot.com/2020/05/get-salesforce-soql-result-in-json.html)