7

To communicate with a REST interface on a private system, I need to send a JSON message from a trigger. I have a message object that has some fields, lists and a child object:

public class MerchantConfig {

public class MIDConfig {
    public String mid;
    public String currencyCode;
    public String cardTemplateName;
    public String paymentPanelRestriction;
    public List<String> restrictedCountries; 
    public String paymentPanelRestrictionForCountries;
}

public String merchantName;
public String sopgTemplateName;
public Integer dispositionTimeWindowInMinutes;
public List<String> features;
public Boolean activateImmediately;
public List<MIDConfig> midConfiguration;
}

I can serialize it to JSON using the built-in serializer:

String body = JSON.serializePretty([MerchantConfig instance]);

The result:

{
  "sopgTemplateName" : null,
  "midConfiguration" : [ {
    "restrictionCountries" : [ "DE", "AT" ],
    "paymentPanelRestrictionForCountries" : "MANDATORY",
    "paymentPanelRestriction" : "OPTIONAL",
    "mid" : "1000000001",
    "currencyCode" : "EUR",
    "cardTemplateName" : null
  }, {
    "restrictionCountries" : [ ],
    "paymentPanelRestrictionForCountries" : "",
    "paymentPanelRestriction" : "OPTIONAL",
    "mid" : "1000000002",
    "currencyCode" : "USD",
    "cardTemplateName" : null
  }, {
    "restrictionCountries" : [ ],
    "paymentPanelRestrictionForCountries" : "",
    "paymentPanelRestriction" : "PROHIBITED",
    "mid" : "1000000003",
    "currencyCode" : "AUD",
    "cardTemplateName" : null
  } ],
  "merchantName" : "Test Merchant",
  "features" : [ XYZ ],
  "dispositionTimeWindowInMinutes" : 10,
  "activateImmediately" : true
}

How can I serialize this object so that all properties that are null don't show up in the JSON string?

In this example, "sopgTemplateName" and all "cardTemplateName" properties are null and should not appear in the JSON string. The empty list "restrictionCountries" should still appear as an empty list, though.

0

3 Answers 3

5

Depending on your use case, you might consider holding your state in a generic Map<String,Object> rather than in specific fields in an Apex class.

You can then use JSON.serialize(Map) which will simply omit the pseudo-fields that have not been added to the Map. Each Object in the Map could be a list or another map or a simple type.


foobarforce.com/.../apex-method-of-the-day-json-serialize-object/

5

This is old thread but it may help someone looking for out-of-box solution. Seems like Salesforce recently updated its API. Please try

String body = JSON.serializePretty([MerchantConfig instance], true);

I have tested it and it works.

serializePretty(objectToSerialize, suppressApexObjectNulls)

Suppresses null values when serializing Apex objects into JSON content and generates indented content using the pretty-print format.

2
  • Thanks for this hint! It's good to see that Salesforce did add this possibility to the JSON methods.
    – georg w.
    Apr 6, 2017 at 7:53
  • wish it could be controlled at the attribute level but at least they added something!
    – NSjonas
    Aug 28, 2017 at 21:33
3

Based on this JSON.Serialize method not returning null fields it looks like the behaviour is version dependent and that API version 25 (probably) strips out the nulls. I suggest you test with API versions from say 24 upwards and see what behaviour you get.

If you do find a version that strips out the nulls, set the class to that API version and add a large comment in your code that the API version is critical and why.

PS See georg w's comment - there is no version as far back as 25 that strips the nulls.

PPS If this is a one-off, then you can write your own serializer for this specific object using the JSONGenerator that includes the null checks.

4
  • I am on version 30, and I tested back to version 25: Null properties are always in the JSON string. Salesforce did not introduce this behaviour that they originally planned for Spring '13, as Josh Kaplan said in the answer that you linked to, and it looks like they did not introduce it later, either.
    – georg w.
    Jun 16, 2014 at 13:19
  • 1
    Sorry to waste your time with that then. If this is a one-off, then you can write your own serializer for this specific object using the JSONGenerator that includes the null checks.
    – Keith C
    Jun 16, 2014 at 13:29
  • I accepted your answer because of your reference to JSONGenerator. This looks like the way to go here.
    – georg w.
    Jun 17, 2014 at 8:57
  • @georgw. Kinda generous but thanks. I added that comment to the end of the answer so it stands out a little more.
    – Keith C
    Jun 17, 2014 at 10:16

You must log in to answer this question.

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