Read More
Read More
Menu Close

    How to implement JSON in Apex Salesforce

    In this blog we will learn about how to implement JSON in apex. JSON class has methods for serializing Apex objects into JSON format and deserializing JSON content that was serialized using the serialize method in this class, all the fields of the related objects are visible. Using the JSONGenerator class methods, you can generate standard JSON-encoded content and you can include the fields that you want to display.

    JSON in Apex

    Apex Class : Write the following code in apex class:

    public with sharing class JSONClass {
        
        /**
         * Webkul Software.
         *
         * @category  Webkul
         * @author    Webkul
         * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
         * @license   https://store.webkul.com/license.html
         **/
        
        public list<Account> acc { get{
        	//Fetch the Account List
        	acc = [Select Id,Name,AccountNumber,AccountSource from Account where name != null limit 50000];
        	
        	if(acc==null){
        		acc = new list<Account>();    		
        	}
        	return acc;
        } set;}   
         
        public string jsonSerial {get{
    		//JSON Class method    
       		if(acc==null){
        		jsonSerial = '';    		
        	}else{
        		jsonSerial = JSON.serialize(acc);
        	}
        	return jsonSerial; 	
        
        } set; }
    
         public string jsonGen { get{
             //JSON Generator Class method 
             JSONGenerator gen = JSON.createGenerator(false);//true gives output in pretty print
            
            if(acc==null){
                return '';          
            }else{
                
                gen.writeStartArray();
                
                for(Account a: acc){
                    
                    gen.writeStartObject();
                        
                        gen.writeStringField('Name', a.name);
                        gen.writeStringField('Id', a.Id);
                        if(a.AccountNumber==null)
                            gen.writeStringField('Account Number', '');
                        else{
                            gen.writeStringField('Account Number',a.AccountNumber); 
                        }
    
                        gen.writeEndObject();
                    
                }
                
            gen.writeEndArray();
            return gen.getAsString();
              
            }
                    
        } set; } 
        
    }

    Visualforce Page : Write the following code in Visualforce Page:

    <apex:page controller="JSONClass">
        
        <!-- 
        /**
         * Webkul Software.
         *
         * @category  Webkul
         * @author    Webkul
         * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
         * @license   https://store.webkul.com/license.html
         */
         -->
    
        <apex:form>
            
            <strong>Output through the list in data table</strong>:<br/><br/>   
            
            <apex:dataTable value = "{!acc}" var = "key">
                
                <apex:column>
                    <apex:facet name = "header">Account Name</apex:facet>
                    {!key.Name}
                </apex:column>
                
                <apex:column>
                    <apex:facet name = "header">Account Number</apex:facet>
                    {!key.AccountNumber}
                </apex:column>
    
            </apex:dataTable>
            
            <br/><br/>
            
            <strong>Output Via JSON serialize method</strong> :<br/><br/> {!jsonSerial}
            
            <br/><br/>
            
            <strong>Output Via JSON Generator method</strong> :<br/><br/> {!jsonGen}
            
        </apex:form>
        
    </apex:page>

    Output

    JSON/JSON generator class in Apex

     

    Searching for an experienced
    Salesforce Company ?
    Read More

    Support

    That’s all for how to implement JSON in Apex , still have any issue feel free to add a ticket and let us know your views to make the code better https://webkul.uvdesk.com/en/customer/create-ticket/

     

    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Table of Content