How to Parse JSON in Java Object using Jackson - Example Tutorial (original) (raw)

Hello guys, if you are wondering how to parse JSON in Java then don't worry, there are many options. In the last article, I have shown you** 3 ways to parse JSON in Java** in this example, You will learn how to parse a JSON String to Java and how to convert Java Object to JSON format using Jackson. JSON stands for JavaScript Object notation is a subset of JavaScript object syntax, which allows all JavaScript clients to process it without using any external library. Because of its compact size, compared to XML and platform independence nature makes JSON a favorite format for transferring data via HTTP.

Though Java doesn't have any inbuilt support to parse JSON responses in the core library, Java developers are lucky to have a couple of good and feature-rich JSON processing libraries such as GSON, Jackson, and JSON-simple.

Jackson is a high-performance, one of the fasted JSON parsing libraries, which also provides streaming capability.

It has no external dependency and solely depends on JDK. It is also powerful and provides full binding support for common JDK classes as well as any Java Bean class, like Player in our case. It also provides data binding for Java Collection classes like Map as well Enum.

Jackson Library

The complete Jackson library consists of 6 jar files that are used for many diffident operations. In this example, we are going to need just one, mapper-asl.jar.

If you want to install the full library to your project you can download and use jackson-all-*.jar which includes all the jars. You can download them from the Jackson Download Page.

Alternatively, If you are using Maven in your project (which you should) then you can add the following dependency in your pom.xml.

org.codehaus.jackson jackson-all 1.9.11

You need Default Constructor in Your Bean Class

When I first run my program, I get the following exception because I had a parametric constructor in the Player class and did not bother to add a no-argument default constructor :

org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class Player]: can not instantiate from JSON object (need to add/enable type information?) at [Source: player.json; line: 1, column: 2] at org.codehaus.jackson.map.JsonMappingException .from(JsonMappingException.java:163) at org.codehaus.jackson.map.deser.BeanDeserializer .deserializeFromObjectUsingNonDefault(BeanDeserializer.java:746) at org.codehaus.jackson.map.deser.BeanDeserializer .deserializeFromObject(BeanDeserializer.java:683) at org.codehaus.jackson.map.deser.BeanDeserializer .deserialize(BeanDeserializer.java:580) at org.codehaus.jackson.map .ObjectMapper._readMapAndClose(ObjectMapper.java:2732) at org.codehaus.jackson.map.ObjectMapper .readValue(ObjectMapper.java:1817) at JSONParser.toJava(Testing.java:30) at JSONParser.main(Testing.java:17)

Once I added the default constructor on the Player class this error is gone. Probably this is another reason why you should have a default or no-arg constructor in the Java class.

How to parse JSON in Java [Example]

Here is our sample program to parse JSON String in Java. As I said, in this example, we will use Jackson, an open-source JSON parsing library with rich features.

There are two static methods here, toJSON() which converts a Java instance to JSON, and fromJSON() method which reads a JSON file, parses it, and creates Java objects.

The key object here is ObjectMapper class from the Jackson library, which is used for converting JSON to Java and vice-versa.

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

import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper;

/**

}

/*

}

Output: Java object converted to JSON String, written to file {"name":"Kevin","sport":"Cricket","age":32,"id":221,"lastScores": [33,66,78,21,9,200]} Java object created from JSON String : Player [name=Kevin, sport=Cricket, age=32, id=221, recent scores=[33, 66, 78, 21, 9, 200]]

This will also create a file called player.json in your current or project directory.

How to Parse JSON to/from Java Object using Jackson Example

That's all about how to parse JSON String in Java and convert a Java object to JSON using Jackson API. Though there is a couple of more good open-source library available for JSON parsing and conversions like GSON and JSON-Simple Jackson is one of the best and feature-rich, its also tried and tested library in many places, which means you should be a little worried about any nasty bug while parsing your big JSON String.

If you like this tutorial and want to know more about how to work with JSON and Java, check out the following fantastic articles :

And, if you are in doubt, always use Jackson to parse JSON in the Java program. Make sure you use the latest version of the Jackson library as well as newer versions always have some bug fixes and performance improvements.

P.S. - If you want to learn more about the advanced Java libraries and concepts then I also suggest you join these Free Java Programming courses, which cover several advanced Java features like JSON, RESTFul Web Services, JAXB, JDBC, etc.