I am getting some very odd behavior from the JSON serializer where if the Sobject was originally instantiated from a SOQL query it will exclude any fields that were not part of my query from the serialized JSON string even if I fill in those fields later in my code.

sample code:

Contact con = [select id, lastname, accountid from Contact limit 1];
string jsonstring = Json.serialize(con);
System.Assert(jsonstring.contains('foo') == false); //this should not be happening

Contact newcon = new Contact(id=con.ID,lastname=con.Lastname,accountid=con.AccountID);
jsonstring = Json.serialize(newcon);

UPDATE: This was fixed in Spring '13!

  • Nothing new here in force.com. Many times we see without including the field in the query if we try some opertion we get errors like field is being refrenced without including in SOQL query Oct 20, 2012 at 5:24
  • 1
    Disagree with Mohith - you can set a field in an sobject even if the field was not in the select. In the example, update con will successfully update the firstname. It is odd that system.debug(con) does show 'foo' as the firstname, whilst system.debug(JSON.serialize(con)) does not. Oct 20, 2012 at 13:19
  • @StephenWillcock I just did this Account acc=[Select Id from Account LIMIT 1]; System.debug('Validate'+acc.Name); I got error of Sobject Row was retrieved without including in query .But you are right when i did use this Account acc=[Select Id from Account LIMIT 1]; acc.Name='Hello'; System.debug('Validate'+acc.Name);This ran so ya i agree its weird little as to why in first case it gave error Oct 20, 2012 at 13:28
  • 2
    Mohith you cannot get a field that you havent selected, but you can set it Oct 20, 2012 at 13:44
  • @StephenWillcock - Actually, I can get the field w/o querying it, but only after I first set it. No errors with this: Contact con = [select id, lastname, accountid from Contact limit 1]; con.firstname='foo'; System.debug('firstName=' + con.firstName);; however take out the con.firstname='foo'; and there will be an error. Oct 20, 2012 at 14:21

2 Answers 2


I do not see anything in the System JSON methods or JSON Parser methods that document the behavior that you observe. I don't see anything in the Getting Started with Apex wiki article. I don't see anything in the SOQL and SOSL guide. I also found nothing in the Salesforce Known Issues

The serialize method documentation is:

Serializes Apex objects into JSON content. The object argument is the Apex object to serialize.

The following example serializes a new Datetime value.

 Datetime dt =
                   2011, 3, 22),
                   1, 15, 18, 0)); 
     String str = JSON.serialize(dt); 

I executed your exact code from Execute Anonymous from my Force.com IDE and observed the exact same behavior.

My conclusion is that I either am not finding the correct documentation, this is the correct behavior and the documentation is lacking, or this is a SF issue.

I also modified your SOQL to:

Contact con = [select id, lastname, firstName, accountid from Contact limit 1];

Then set the first name the same:

con.firstName = 'foo';

This does result in 'foo' being in the jsonstring.

Obviously, the workaround is to always select all of the fields that you need to appear in the jsonstring or construct the new instance like you did.

  • @PeterKnolle Thanks for all the legwork. I think it's a sf bug as well but I'm going to leave this open for a bit to see if we can get an "official" sf response. Oct 20, 2012 at 20:17
  • @grigriforce - Good idea. Two of us confirm the behavior, so it's definitely not just something in your environment. Oct 20, 2012 at 20:21
  • This is much better than the answer I was going to give; this must be a Salesforce bug, you'll need to log a case Oct 21, 2012 at 6:00

From speaking with salesforce support this is a known issue with no current time frame for when this will be fixed.

As Peter said the only workaround is to include all the fields in the query that you want to show up in the JSON.

UPDATE: This was fixed in Spring '13!

You must log in to answer this question.

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