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
Hitesh AlgamwarHitesh Algamwar 

I have one rest resource class where in the post method i need to return response in json format.

Below is my program :- 

@RestResource(URLMapping='/SendEmailForOTP/*')
global class WordpressIntegrationForEmail {
    
    @HTTPPOST
    global static FinalReturnWrapper SendEmailMsg(string email , string otp,string mob,boolean send_otp_if_exists)
    {
        
        FinalReturnWrapper returnResponse = new FinalReturnWrapper(); 
        cls_data objData = new cls_data();
        contact_data cobjData = new contact_data();
        string failed = 'Email not exist';   //Need to create custom label
        string success = 'Email successfully sent to email address' ;//Need to create custom label
        list<contact> conList = [Select id,Do_Not_Mail__c,Email_Verification_Status__c, Email_Verification_Status_Date__c from contact where email =: email Limit 1];
        
        if(conList.size() > 0 )  // If the email id is exist then ...
        {
            objData.contact_exists = true;
            
            for(contact c : conList)
            {
                objData.contact_id =c.id;
                cobjData.name=c.Name;
                if(c.Do_Not_Mail__c == false && c.Email_Verification_Status__c == null && c.Email_Verification_Status_Date__c == null)
                {
                    
                    OtpViaEmail.sendEmail(email,otp);
                    objData.otp_sms_sent = true; 
                    objData.otp_email_sent = true;
                    
                }
                
            }
            returnResponse.obj = objData;
            returnResponse.cobj= cobjData ;
            return returnResponse;
            
        }
        else    // If the email id not exist then...
        {
            if(send_otp_if_exists ==false )
            {
                OtpViaEmail.sendEmail(email,otp);
                objData.otp_sms_sent = true; 
                objData.otp_email_sent = true;
                
                
                return returnResponse;
            }
            
            
        }
        
    }
    
    global class FinalReturnWrapper {
        
        public cls_data obj ;
        public contact_data cobj;
        
    }
    public class cls_data {
        
        public boolean contact_exists;
        public boolean otp_sms_sent;
        public boolean otp_email_sent;
        public string contact_id;
        
    }
    
    public class contact_data {
        public string name;
        
        
    }
    
    
}


See here i need to return FinalReturnWrapper but when i called this from postman so getting error as parser error.

So can someone please let me know how can I change the response to json file. Please guid me the code.

Shivdeep KumarShivdeep Kumar
Hi Hitesh,
You can use the JSON generator to return the response as JSON. Please see the sample code below-
List<Contact> conList = [Select ID,Name,Do_Not_Mail__c,Email_Verification_Status__c,Email_Verification_Status_Date__c From Contact Where Email =: Email LIMIT 1];
if(!conList.isEmpty()){
JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();     
    gen.writeFieldName('finalReturnWrapper');
for(Contact con : conList){
gen.writeStartObject();
gen.writeFieldName('cls_data');
        gen.writeBooleanField('contact_exist', true);
        gen.writeStringField('contact_id', String.valueOf(con.Id));
if(!con.Do_Not_Mail__c && con.Email_Verification_Status__c == null && con.Email_Verification_Status_Date__c == null){
        gen.writeBooleanField('otp_sms_sent', true);
        gen.writeStringField('otp_email_sent', true);
}else{
 gen.writeBooleanField('otp_sms_sent', false);
        gen.writeStringField('otp_email_sent', false);
}
        gen.writeEndObject();
gen.writeStartObject();
gen.writeFieldName('contact_data');
gen.writeStringField('name', String.valueOf(con.Name));
gen.writeEndObject();
}
    gen.writeEndObject();
    String jsonData = gen.getAsString();
    System.debug('jsonData-' + jsonData);
}

Please refer the link for more details- JSON Generator  (https://developer.salesforce.com/docs/atlas.en-us.242.0.apexref.meta/apexref/apex_class_System_JsonGenerator.htm)
Let me know if this helps!

Thanks,
Shivdeep