I want to serialize just the Address object (lets call it an object) of Account (and Contact as well)

Is there a way to access the ShippingAddress/BillingAddress of the account using the JSON.serialize method?

1 Answer 1


Address information is surfaced in two ways on these objects...

  • Address Fields. Using the historic standard set of fields on the source object (e.g. Account or Contact). Where XXX is Shipping or Billing, the standard set of fields are XXXCity, XXXCountry, XXXCountryCode, XXXPostalCode, XXXState, XXXStateCode, XXXStreet. These fields are available via SOAP/REST API and from within Apex.
  • Address Compound Type. And now also through a new Address Compound Type (new to Spring'14 in Beta). This does in fact give you a single object/data type that wraps all the address fields. It however is only available read-only and then only through the Salesforce SOAP/REST API's, and not via Apex.

    Standard addresses—addresses built into standard objects in Salesforce—are accessible in the SOAP and REST APIs as an Address, a structured compound data type, as well as individual address elements.

So if you are within Apex you will have to develop your own Apex data type and serialise that. I have developed a native Apex Address type here. To use it, it's a simple as calling it's valueOf method.

    // Query Account and approprite fields
    Account myAccount = 
        [select Id, 
         from Account
         where Name = 'Burlington Textiles Corp of America' limit 1];

    // Create a Billing Address
    Address billingAddress = 
        Address.valueOf(myAccount, Address.AccountAddress.Billing);

    // Serialise it via JSON
    String jsonBillingAddress = JSON.serialize(billingAddress);

This outputs...

11:35:55.344 (344137285)|USER_DEBUG|[22]|DEBUG|{"Street":"525 S. Lexington Ave","StateCode":null,"State":"NC","PostalCode":"27215","CountryCode":null,"Country":"USA","City":"Burlington"}

NOTE: The Address type also supports the State and Country pick list feature if enabled in the org.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .