Checkout for Promo Codes

How to read specific json node in Jackson api (tree model)?

This page shows how to read Json data in tree model using JsonNode.

Note: Refer How to convert Java object to JSON string? page for dependent libraries.

Here is the input json file:

Json Input:
    "emp_id": 1017,
    "emp_name": "Nagesh Y",
    "emp_designation": "Manager",
    "department": "Java2Novice",
    "salary": 30000,
    "direct_reports": [
        "Nataraj G",

package com.java2novice.json.examples;

import java.util.Iterator;

import org.codehaus.jackson.JsonNode;

public class JsonTreeModleReadEx {

	public static void main(String a[]){
		ObjectMapper mapper = new ObjectMapper();
		try {
			// reading json input from the file and mapping to object
			File jsonInputFile = new File("/Users/java2novice/jsonInput.txt");
			JsonNode rootNode = mapper.readTree(jsonInputFile);
			// read employee id
			JsonNode empId = rootNode.path("emp_id");
			// read employee name
			JsonNode empName = rootNode.path("emp_name");
			// read direct reports
			JsonNode drNode = rootNode.path("direct_reports");
			Iterator<JsonNode> itr = drNode.getElements();
			System.out.println("\nDirect reports:");
			while (itr.hasNext()) {
				JsonNode temp =;
		} catch (IOException e) {
			// TODO Auto-generated catch block

Nagesh Y

Direct reports:
Nataraj G
<< Previous Program | Next Program >>

Jackson JSON examples

  1. How to convert Java object to JSON string?
  2. How to convert JSON string to Java object?
  3. How to convert JSON string to Map using Jackson API?
  4. How to convert Map to JSON string using Jackson API?
  5. Enable JSON pretty print using Jackson API
  6. How to rename JSON properties using Jackson annotations?
  7. How to ignore JSON property using Jackson annotations?
  8. How to order JSON elements using Jackson annotations?
  9. How to ignore json empty or null values using Jackson API in java?
  10. How to handle date in Json using Jackson api in java?
  11. How to read specific json node in Jackson api (tree model)?
  12. Jackson API client - how to read json from URL?
Knowledge Centre
Difference between Enumeration and Iterator
The functionality of Enumeration and the Iterator are same. You can get remove() from Iterator to remove an element, while while Enumeration does not have remove() method. Using Enumeration you can only traverse and fetch the objects, where as using Iterator we can also add and remove the objects. So Iterator can be useful if you want to manipulate the list and Enumeration is for read-only access.
Famous Quotations
Tomorrow is often the busiest day of the week.
-- Spanish Proverb

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2020 by Nataraja Gootooru. All Rights Reserved.