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
Lawrence-AccentureLawrence-Accenture 

JSON.serialize() includes null values for Apex objects

When serializing Apex objects, JSON.serialize() still includes null class variables even though the documentation seems to claim this was fixed back in Apex version 28. (I've tested with versions up to the current version -- 36)

Example Apex class:
public with sharing class xtmp_TestJSON {
    public String SomeText;
    public String SomeOtherText;
}
and test JSON.serialize() using:
xtmp_TestJSON req = new xtmp_TestJSON();
req.SomeText = 'Hi';

System.debug('====> data: ' + JSON.serialize(req));
Output is:
====> data: {"SomeText":"Hi","SomeOtherText":null}

Any suggestions on how to "fix" this?

Thanks,

-L

 
Lawrence-AccentureLawrence-Accenture
Nevermind... appears to be a common problem known by other developers: https://success.salesforce.com/ideaView?id=08730000000DkDhAAK
Claudio Chiaro 12Claudio Chiaro 12

Have "null" can be a problem when you start to use the serialized json in a callout. You can get error from the receiver.

My suggestion is to use JSON.serialize(req,true); the second parameter suppress the nulls.

Lawrence CoffinLawrence Coffin
Thanks Claudio. Looks like they've added support for that recently. Good to know!

-L