I've currently got a use case where I would like to JSON serialize an SObject, but include fields with null (blank) values.
According to the documentation (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_Json.htm), using JSON.serialize(objectToSerialize, suppressApexObjectNulls)
should give me the option to suppress null values or not. But it doesn't seem to be the case for SObjects. Take this example:
Account account = [SELECT Id, Name, AccountNumber FROM Account WHERE Id = '001b000000Irj49'];
system.debug(JSON.serialize(account, false));
Returns:
{
"attributes": {
"type": "Account",
"url": "\/services\/data\/v42.0\/sobjects\/Account\/001b000000Irj49AAB"
},
"Id": "001b000000Irj49AAB",
"Name": "Ben"
}
Where the AccountNumber field for my test record is null, but I would actually like it to be in my JSON.
However, if I run:
Account account = new Account(Name = 'Ben', AccountNumber = null);
system.debug(JSON.serialize(account, false));
I get:
{
"attributes": {
"type": "Account"
},
"Name": "Ben",
"AccountNumber": null
}
So it seems like I would only get a null value if I explicitly assign null to the SObject.
Obviously I can iterate over each field in the object and build the JSON structure that way, but I would prefer to keep it a simple one line. My other test case is using a wrapper class, which seems to work when toggling suppressApexObjectNulls
.
{}
button for proper formatting.