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
Behzad Bahadori 18Behzad Bahadori 18 

how to make custom object in Apex JSon format like javascript object

I have a javascript object that looks like this
this will be used in an ajax called in javascript  data: JSON.stringify(address) and submitted
var address = { location: { 
                                    phoneNumber: "", 
                                    address: {
                                    line1: $( "line1" ).val(),
                                    city: $( "city" ).val(), 
                                    state: $( "state" ).val(), 
                                    postalCode: $( "postalCode" ).val(),
                                    country: "US", 

                                            }
                                  }, 
                        networks: ["1", "2","3","4","5"],  
                        company: "Salesforce"
                      };

what I need to do is samething but in apex code with custom object but not sure how I can format it same way

qualRequest = new prequalAPI.QualificationRequest();   
qualRequest.CustomerId = 0;
            qualRequest.PhoneNumber = 'STANDALONE';
            qualRequest.AddressLine1 = addressLine1;

            if (unitType != null)
            {
                qualRequest.UnitType = unitType;
            }

            if (unitValue != null)
            {
                qualRequest.UnitValue = unitValue;
            }

            qualRequest.City = city;
            qualRequest.State = state;
            qualRequest.ZipCode = zip;
            qualRequest.Country = country;
            qualRequest.UnitType = unitType;
            qualRequest.RequestedNetworkNames = new List<String>();
            qualRequest.RequestedNetworkNames.add('1');
            qualRequest.RequestedNetworkNames.add('2');
            qualRequest.RequestedNetworkNames.add('3');
            qualRequest.RequestedNetworkNames.add('4');
            qualRequest.RequestedNetworkNames.add('5');

JSON.serialize(qualRequest);
my request status fails right now because json is not same format. 
Best Answer chosen by Behzad Bahadori 18
LBKLBK
Hi Behzad,

Defining a class structure, populating an instance of it and serializing the object would be ideal way of getting the JSON output.

Try the code below.

Feel free to add more attributes as you need.
 
public class clsJSONGenerator {

    public static String generateJSON(){
        //Sample Data
        String sCompany = 'Salesforce';
        String sLine1 = 'One Market, Suite 300';
        String sCity = 'San Francisco';
        String sState = 'CA';
        String sPostalCode = '94105';
        String sCountry = 'US';
        String sPhoneNumber = '415–901–7000';
        List<String> lstNetwork = new List<String>{'0','1','2','3','4','5'};
        //Sample Data
    
        RootObject objJSONRoot = new RootObject();
        
        Address objAddress = new Address();
        objAddress.line1 = sLine1;
        objAddress.city = sCity;
        objAddress.state = sState;
        objAddress.postalCode = sPostalCode;
        objAddress.country = sCountry;
        
        Location objLocation = new Location();
        objLocation.phoneNumber = sPhoneNumber;
        objLocation.address = objAddress;        
        
        objJSONRoot.location = objLocation;
        objJSONRoot.networks = lstNetwork;
        objJSONRoot.company = sCompany;
        
        String sJSONOutput = JSON.serialize(objJSONRoot);
        System.debug(sJSONOutput);
    	return sJSONOutput;
    }    
	public class Address
	{
		public string line1 { get; set; }
		public string city { get; set; }
		public string state { get; set; }
		public string postalCode { get; set; }
		public string country { get; set; }
	}

	public class Location
	{
		public string phoneNumber { get; set; }
		public Address address { get; set; }
	}

	public class RootObject
	{
		public Location location { get; set; }
		public List<string> networks { get; set; }
		public string company { get; set; }
	}

}
Let me know if this helps.
 

All Answers

Tarun J.Tarun J.
Hello Behzad,

You need to generate inner class to replicate your JSON structure in apex code and populate values. Refer below sample code:
 
public class Address{
    public string line1 { get; set; }
    public string city { get; set; }
    public string state { get; set; }
    public string postalCode { get; set; }
    public string country { get; set; }
}

public class Location{
    public string phoneNumber { get; set; }
    public Address address { get; set; }
    public List<string> networks { get; set; }
    public string company { get; set; }
}

Address add = new Address();
add.city = city;
add.state = state;
add.postalcode = zip;
add.country = country;

Location loc = new Location();
loc.address = add;
loc.company = 'Salesforce';
loc.network.add('1');
loc.network.add('2');
loc.network.add('3');
loc.network.add('4');
loc.network.add('5');

String strJSON = JSON.serialize(loc);

System.debug(strJSON);

-Thanks,
TK

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
LBKLBK
Hi Behzad,

Defining a class structure, populating an instance of it and serializing the object would be ideal way of getting the JSON output.

Try the code below.

Feel free to add more attributes as you need.
 
public class clsJSONGenerator {

    public static String generateJSON(){
        //Sample Data
        String sCompany = 'Salesforce';
        String sLine1 = 'One Market, Suite 300';
        String sCity = 'San Francisco';
        String sState = 'CA';
        String sPostalCode = '94105';
        String sCountry = 'US';
        String sPhoneNumber = '415–901–7000';
        List<String> lstNetwork = new List<String>{'0','1','2','3','4','5'};
        //Sample Data
    
        RootObject objJSONRoot = new RootObject();
        
        Address objAddress = new Address();
        objAddress.line1 = sLine1;
        objAddress.city = sCity;
        objAddress.state = sState;
        objAddress.postalCode = sPostalCode;
        objAddress.country = sCountry;
        
        Location objLocation = new Location();
        objLocation.phoneNumber = sPhoneNumber;
        objLocation.address = objAddress;        
        
        objJSONRoot.location = objLocation;
        objJSONRoot.networks = lstNetwork;
        objJSONRoot.company = sCompany;
        
        String sJSONOutput = JSON.serialize(objJSONRoot);
        System.debug(sJSONOutput);
    	return sJSONOutput;
    }    
	public class Address
	{
		public string line1 { get; set; }
		public string city { get; set; }
		public string state { get; set; }
		public string postalCode { get; set; }
		public string country { get; set; }
	}

	public class Location
	{
		public string phoneNumber { get; set; }
		public Address address { get; set; }
	}

	public class RootObject
	{
		public Location location { get; set; }
		public List<string> networks { get; set; }
		public string company { get; set; }
	}

}
Let me know if this helps.
 
This was selected as the best answer
Behzad Bahadori 18Behzad Bahadori 18
Thank you so much that was a great help