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

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){
public void getcontactdetails(){
contactlist= [select Id,AccountId,Name From contact WHERE AccountId In:accountids];
 system.debug('ContactList:' +contactlist);


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

Please go through the following links

Please mark as best answer, if the above helps ....!!!!!
This blog helped me to solve my requierment same as yours.
SOQL result in JSON (