I am calling an GET API, where is am getting account details like name, number, ext Id etc. For parsing the response, i created a class like below.

public class AccWrp {
    public String customer_no;
    public String customer_name;
    public String extId;

Now in one post call i need to send Account details(after upsert) with some extra fields, like isActive, Email, Phone, Fax. I thought i will use same class for sending data back, as they are expecting same attributes in the JSON as above. So i added other fields as well.

public class AccWrp {
    public String customer_no;
    public String customer_name;

    //used in post call
    public String email;
    public String phone_no;
    public String fax_no;
    public Boolean is_active;


But when i am using this AccWrp class instance in post call. Properties which are not used is going as null. like

"customer_name": null, "customer_no" : null

Is there any way i can have only one class for parsing and for POST call without sending other properties as null.

Is there any way, i can have only one class?

Thanks, Manohar

1 Answer 1


When making your JSON, pass in "true" as the second parameter to omit null values.

AccWrp a = new AccWrp();
// ...
String body = JSON.serialize(a, true);
// ...
  • thank you so much for your response. Passing true is omitting all the null values. Is there any way i can keep any specific value? can i send two properties in one response and other two in next response, even though its null. It should appear but properties should be specific to one API call. but i want to keep all my AccountWrp properties in one class. And send based on my API call. Is it possible?
    – Manohar
    Feb 3, 2020 at 14:00
  • @Manohar No, it's all or none if you just use a class this way. You either need multiple classes, or you might consider just using a Map<String, String> instead, which you can serialize either way as you desire.
    – sfdcfox
    Feb 3, 2020 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.