How to search a value inside a JSON file using Jackson in Java?

JavaJSONObject Oriented ProgrammingProgramming

The com.fasterxml.jackson.databind.node.ObjectNode class can be used to map the JSON object structure in Json content. We can search for a particular value inside the JSON file using the get() method of ObjectNode class, this method used for accessing the value of a specified field of an object node.

Syntax

public JsonNode get(String fieldName)

Example

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
public class ObjectNodeTest {
   public static void main(String args[]) throws Exception {
      String jsonString = "{\"Id\":101, \"name\":\"Raja Ramesh\", \"address\":\"Madhapur\"}";
      ObjectMapper mapper = new ObjectMapper();
      ObjectNode node = mapper.readValue(jsonString, ObjectNode.class);
      if(node.has("name")) {
         System.out.println("NAME: " + node.get("name"));
      }
   }
}

Output

NAME: "Raja Ramesh"
raja
Published on 19-Nov-2019 08:57:33
Advertisements