Json Tree Model Example using Jackson API

This post introduces Json Tree Model Example using Jackson API. Actually, JSON can be represented as a Tree (similar to DOM tree). Therefore, we are possible to traverse to individual nodes in the tree in order to access/edit their values.

Other interesting posts you may like

Let’s start to build Json Tree Model Example

JACKSON dependency in pom.xml

There are several jar in JACKSON library. For this example, we are only using databind.

Jackson framework provides api to do it. JsonNode refers to an individual node in tree which can be accessed using node name. And we are using readTree and writeTree of ObjectMapper object to read and write JSON tree.

Let’s see how it can be done in Java

Json input

We will preparing the file fruit.json with content such as

{“car”:”Audi”,”model”:”2010″,”price”:”30000″,”colors”:[“Grey”,”White”,”Black”]}

Reading JSON Tree using Tree Model

Let’s see the below code snippet to know read Json tree using tree model

Here is the output:

Json Tree Model Example

Writing JSON Tree using Tree Model

Here is the output

Json Tree Model Example screenshot

That’s it. You also refer to other post Convert Java Object to/from JSON using JACKSON API

Leave a Comment

*

Please share it if you found this useful
Hide Buttons