title: Jackson Deserializing JSON, skip objects that don't have required fields?
body: <p>Using Jackson to deserialize a JSON response into a DTO.</p>
  
  <p>For a given response:</p>
  
  <pre><code>[ {
      foo : "bar",
      value : "hello world"
  },
  {
      foo : "bar",
      value : "hello world"
  },
  {
      foo : "bar",
      value : "hello world"
  },
  {
      foo : "bar",
  } ]
  </code></pre>
  
  <p>You can see that all the objects in this array but one of them is inconsistent in the schema (the last one).</p>
  
  <p>After looking at some other related questions on StackOverflow:</p>
  
  <p><a href="https://stackoverflow.com/questions/12253233/deserializing-json-using-jackson-with-missing-fields">deserializing json using jackson with missing fields</a></p>
  
  <p><a href="https://stackoverflow.com/questions/36452668/ignore-null-fields-when-deserializing-json-with-gson-or-jackson">Ignore null fields when DEserializing JSON with Gson or Jackson</a></p>
  
  <p>They still create an object from that irregular JSON object.</p>
  
  <p>Which means I need to then iterate through this list and delete any objects that do not have the attribute "value" by implementing a cleaning method.</p>
  
  <p>What I want to know is can Jackson do this business logic for me?</p>
  
  <pre><code>If this JSON object has all the required fields
  Then create a new DTO
  Else
  Skip to next JSON object in response
  </code></pre>
  
  <p>My DTO with Jackson annotations:</p>
  
  <pre><code>    @JsonIgnoreProperties(ignoreUnknown = true)
      @JsonInclude(JsonInclude.Include.NON_NULL)
      public final class FooDTO {
  
          private final String foo;
          private final String value;
  
          /**
           * Constructor
           *
           * @param foo           String
           * @param value String
           */
          @JsonCreator
          public FooDTO(@JsonProperty("foo") final String foo, @JsonProperty("value") final String value) {
              this.foo = checkNotNull(foo, "foo required");
              this.value = value;
          }
  
          @JsonProperty("foo")
          public void setFoo(final String foo) {
              this.foo = foo;
          }
  
          /**
           * @return String
           */
          public String foo() {
              return foo;
          }
  
      etc...
  </code></pre>
  
  <p>With the result from the given JSON response being 3 DTOs being initialised, rather than 4.</p>