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



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"
Updated on: 2020-07-09T07:52:12+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements