title: Java Optional Null Json Field
body: <p>I am using the JsonNode objects from the Jackson library to hold json responses. I could potentially have a json like so:</p>
  
  <pre><code>{
      "a": "test",
      "b": true
  }
  </code></pre>
  
  <p>However, at times the <code>b</code> field could be missing. So it could come back like this:</p>
  
  <pre><code>{
      "a": "test"
  }
  </code></pre>
  
  <p>In that case I want to return <code>Optional.isEmpty()</code> when I'm trying to retrieve its value. Essentially this is the java code for that:</p>
  
  <pre><code>if(json.path("b").isMissingNode()) {
      return Optional.empty();
  } else {
      return Optional.of(json.get("b").asBoolean());
  }
  </code></pre>
  
  <p>However, I want to condense this into 1 line using <code>.filter</code> and <code>.ofNullable</code> but I'm not exactly sure how to do this. I found <a href="https://stackoverflow.com/questions/28322464/in-java-8-transform-optionalstring-of-an-empty-string-in-optional-empty">this post</a> that shows an example of how to use the filter but I understand what the <code>s</code> is....</p>
  
  <p>Basically I want to find the field labeled <code>b</code>, if it's not there then return empty. If it is there then return the boolean value of it's field.</p>
  
  <p>Edit: This is not a duplicate because I am not serializing the entire json into an object. I don't have any need for the JsonIgnore annotations. I only want to extract 1 value. Which is like above.</p>