JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

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",
        "Kalyan",
        "Mahitha"
    ]
}

package com.java2novice.json.examples;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;

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");
			System.out.println(empId.getIntValue());
			// read employee name
			JsonNode empName = rootNode.path("emp_name");
			System.out.println(empName.getTextValue());
			// read direct reports
			JsonNode drNode = rootNode.path("direct_reports");
			Iterator<JsonNode> itr = drNode.getElements();
			System.out.println("\nDirect reports:");
			while (itr.hasNext()) {
				JsonNode temp = itr.next();
				System.out.println(temp.getTextValue());
 			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

Output:
1017
Nagesh Y


Direct reports:
Nataraj G
Kalyan
Mahitha
<< 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
Pass by value Vs Pass by reference
Pass by value: Passing a copy of the value, not the original reference.

Pass by reference: Passsing the address of the object, so that you can access the original object.
Famous Quotations
It is easier to fight for one’s principles than to live up to them.
-- Alfred Adler

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 © 2022 by Nataraja Gootooru. All Rights Reserved.