I have an issue. I have a class with few Strings

public class DopplerJSONSerializer {

    public String name;
    public String email;

In some, I need to serialize and use only the name String in some methods, and in others I need to use all of them.

Right now if I do for example:

public String createListJSON(String listName) {
    DopplerJSONSerializer listWrapper = new DopplerJSONSerializer();
    listWrapper.name = listName;
    String listJSON = JSON.serialize(listWrapper);
    return System.JSON.serialize(listJSON);

It will return me:

{"name":"New List", "email":null}

How can I do to not include the non serialized variables into the JSON.serialize?

Thank you

1 Answer 1


Use the alternative method signature:

serialize(objectToSerialize, suppressApexObjectNulls);

Using your code as an example:

String listJSON = JSON.serialize(listWrapper, true); // suppress null values
  • Thank you, dunno why after all the search about json.serialize I didnt saw anywhere the explanation about the boolean in the method,
    – BoDiE2003
    Mar 16, 2017 at 19:42
  • I've gotten into the habit of searching for the actual Class name, e.g. 'Salesforce JSON Class'. Those results sometimes lack detailed examples, but I'll often stumble across a little-used but just-what-i-need method or overload. For example the JSON Class. Mar 17, 2017 at 20:19

You must log in to answer this question.

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