title: Extract certain fields and values from request JSON using Jackson
body: <p>I am looking for a way to include only certain fields from the JSON request received to my service and then log them accordingly to avoid logging too much data.</p>
  
  <p>The fields to include and hence log would be configured in a properties file.</p>
  
  <p>Since I use Spring Boot for my service, Jackson JARs should already be available.</p>
  
  <p>Since the request JSON is complex with nested arrays and objects as fields am not sure if I can achieve my requirement with Jackson.</p>
  
  <p>Is there a way to extract only certain fields along with their values from a input request JSON using the Jackson API?</p>
  
  <p>Basically, I am looking at 2 use cases.</p>
  
  <p>1.Select one or more elements (which I intend to pass by config) from Json String and then render them</p>
  
  <p>2.Select and update one or more elements inline. I want to mask the values of the elements before rendering them.</p>
  
  <p>I am providing the code for selecting the element and Json which I used along with what I expect as below.</p>
  
  <pre><code>public String getValues(String key,String jsonString){
      String fieldNodeStr =null;
      try {
          ObjectMapper mapper = new ObjectMapper();
          JsonNode node = mapper.readTree(jsonString);
          JsonNode fieldNode = node.at(key);
          fieldNodeStr = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(fieldNode);
          System.out.println(fieldNodeStr);
      } catch (IOException e) {
          System.out.println("IOException:",e);
      }catch (Exception e) {
          System.out.println("Exception:",e);
      }
  }   
  </code></pre>
  
  <p>My Json is as below,</p>
  
  <pre><code>{
      "employeeId" : "5353",
      "salesId" : "sales005",
      "manager" : {
          "userId" : "managerUser",
          "isSuperUser" : false,
          "firstName":"manager first name",
          "lastName":"manager last name"
              },
      "administrator" : {
          "userId" : "administratorUser",
          "isSuperUser" : false,
          "firstName":"admin first name",
          "lastName":"admin last name"
      },
      "requester" : [
                      {
                              "id":"1234",
                              "demographic" : {
                                                  "firstName" : "hello",
                                                  "lastName" : "hai"
                                               }
                       },
                       {
                              "id":"1235",
                              "demographic" : {
                                                  "firstName" : "welcome",
                                                  "lastName" : "user"
                                              }
                        }
                     ]
  
  }
  </code></pre>
  
  <p>I have 2 issues as below.</p>
  
  <p>If I pass "/manager/userId" ,"/administrator/isSuperUser" (OR) "/salesId" I am able to get the expected value.</p>
  
  <p>But, If want to get the /requester/id (OR) /requester/demographic (OR) /requester/demographic/lastName , I am not able to fetch. 
  I am getting null values. </p>
  
  <p>I expect the following when I pass , "/requester/id" (OR) "/requester/demographic" respectively.</p>
  
  <pre><code>"requester" : [
                      {
                              "id":"1234"
                       },
                       {
                              "id":"1235"
                        }
                  ]
  
  
   "requester" : [
                      {
                              "demographic" : {
                                                  "firstName" : "hello",
                                                  "lastName" : "hai"
                                               }
                       },
                       {
                              "demographic" : {
                                                  "firstName" : "welcome",
                                                  "lastName" : "user"
                                              }
                        }
                     ]
  </code></pre>
  
  <p>Along with fetch I also want to update the values inline after locating them with JsonPointer</p>
  
  <p>I have my code as below for the updation,</p>
  
  <pre><code>public String findAndUpdate(String key,String jsonString,String repValue){
      String fieldNodeStr =null;
      try {
          ObjectMapper mapper = new ObjectMapper();
          JsonNode node = mapper.readTree(jsonString);
          JsonNode fieldNode = node.at(key);
  
          //Update the value of located node to a different one
          ((ObjectNode) fieldNode).put(key,repValue);
  
          fieldNodeStr = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(fieldNode);
          System.out.println(fieldNodeStr);
      } catch (IOException e) {
          System.out.println("IOException:",e);
      }catch (Exception e) {
          System.out.println("Exception:",e);
      }
      return fieldNodeStr;
  }
  </code></pre>
  
  <p>When I pass, "/manager/userId" as value of key, I get the below error,</p>
  
  <pre><code>17:21:24.829 [main] ERROR com.demo.jsondemo.TestClass - Exception:
  java.lang.ClassCastException: com.fasterxml.jackson.databind.node.TextNode cannot be cast to com.fasterxml.jackson.databind.node.ObjectNode
      at com.demo.jsondemo.TestClass.findAndUpdate(TestClass.java:95) [classes/:na]
      at com.demo.jsondemo.TestClass.main(TestClass.java:221) [classes/:na]
  </code></pre>