title: Ignore empty elements from json body using jackson
body: <p>I am constructing a json body using jackson in java. It would be something like the below </p>
  
  <pre><code>{
     "subject": "math",
      "marks": "100",
      "student":{
          "name": "x",
          "class": "8"
      }
   }
  </code></pre>
  
  <p>Based on different REST URI's the json body has to ignore some fields or elements. How do I Ignore the "student" part from the above json body using jackson? 
  When I ignore it I should be able to get only </p>
  
  <pre><code>{ "subject": "math", "marks": "100"}
  </code></pre>
  
  <p><strong>but am getting it as below which is incorrect-</strong></p>
  
  <pre><code>{ "subject": "math", "marks": "100","student":{}}
  </code></pre>
  
  <p>I have two classes with getters and setters, one as subject and the other as student. I tried using @JsonIgnore but it ignores all the URI's, which I don't want. I also tried @JsonInclude(Include.NON_EMPTY). How do I achieve this?</p>
  
  <p>Am adding my code here. Based on different REST URI's the json body has to ignore some fields or elements.Example for one URI, it should include all the fields , for another it should ignore student. 
         Score score = new Score();</p>
  
  <pre><code>    score.setSubject("math");
      score.setMarks("100");
  
      Score.Student student =score.new Student();
      score.setStudent(student);
  
      switch (type) {
      case StudentAdd:
          score.setSubject("math");
          score.setMarks("100");
          break;
      case StudentDelete:
          score.setSubject("math");
          score.setMarks("100");
          break;
      case StudentComplete:
          score.setChangeReason("C");
  
      default:
          break;
          score.setSubject("math");
          score.setMarks("100");
  
      score.setStudent(student);
  
      ObjectMapper objectMapper = new ObjectMapper();
      objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
      //objectMapper.setSerializationInclusion(Include.NON_NULL);
      objectMapper.setSerializationInclusion(Include.NON_EMPTY);
  
      StringWriter jsonBody = new StringWriter();
      objectMapper.writeValue(jsonBody, score);
  
      return jsonBody.toString();
  }
  
      //@JsonInclude(Include.NON_EMPTY)
      class Score {
  
          private String subject;
          private String marks;
          private Student student;
  
          public String getSubject() {
              return subject;
          }
  
          public void setSubject(String subject) {
              this.subject = subject;
          }
  
          public String getMarks() {
              return marks;
          }
  
          public void setMarks(String marks) {
              this.marks = marks;
          }
  
          public Student getStudent() {
              return student;
          }
  
          public void setStudent(Student student) {
              this.student = student;
          }
  
      //@JsonInclude(Include.NON_NULL)
      class Student {
  
          private String name;
  
          @JsonProperty("class")
          private String clazz;
  
          public String getName() {
              return name;
          }
  
          public void setName(String name) {
              this.name = name;
          }
  
          public String getClazz() {
              return clazz;
          }
  
          public void setClazz(String clazz) {
              this.clazz = clazz;
          }
  
  }
      }
     }
       }
  </code></pre>