Home » Core Java » json » Jackson » Jackson Tree Model example

About Nikos Maravitsas

Nikos Maravitsas
Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.

Jackson Tree Model example

In this example we are going to see how to use Jackson Tree Model. This model is very similar in many ways with the DOM representation. We are going to see how to create the tree model and update the JSON object with new values.
 
 
 
 
 
 
 
 
 
Here is the file we are going to use for the demo:

mapExample.json:

{"names":["Jonh","Jack","James"],"interest":"Java","domain":"JavaCodeGeeks.com","members":400}

JacksonTreeNodeTutorial.java:

package com.javacodegeeks.java.core;

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

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.node.ObjectNode;

public class JacksonTreeNodeTutorial {

	private static final String jsonFilePath = "C:\\Users\\nikos7\\Desktop\\filesForExamples\\mapExample.json";

	public static void main(String[] args) {

		ObjectMapper mapper = new ObjectMapper();

		try {

			FileReader fileReader = new FileReader(jsonFilePath);

			BufferedReader bufferedReader = new BufferedReader(fileReader);

			JsonNode rootNode = mapper.readTree(bufferedReader);

			JsonNode domainNode = rootNode.path("domain");

			System.out.println("domain : "+domainNode.getTextValue());

			JsonNode interestNode = rootNode.path("interest");

			System.out.println("interest : " + interestNode.getTextValue());

			JsonNode memebers = rootNode.path("members");

			System.out.println("members : "+memebers.getIntValue());

			JsonNode namesListNode = rootNode.path("names");

			System.out.println("names : ");

			Iterator iterator = namesListNode.getElements();

			while (iterator.hasNext()) {

				JsonNode temp = iterator.next();
				System.out.println(" "+temp.getTextValue());

			}

			((ObjectNode)rootNode).put("domain", "wwww.javacodegeeks.com");

			((ObjectNode)rootNode).put("members", 600);

			((ObjectNode)rootNode).remove("interest");

			File file = new File(jsonFilePath);

			mapper.writeValue(file, rootNode);

		} catch (JsonGenerationException e) {

			e.printStackTrace();

		} catch (JsonMappingException e) {

			e.printStackTrace();

		} catch (IOException e) {

			e.printStackTrace();

		}

	}
}

mapExample.json:

{"names":["Jonh","Jack","James"],"domain":"wwww.javacodegeeks.com","members":600}

 
This was an example on Jackson Tree Model.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

 

Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.