Sunday, August 1, 2021

How to get JAR files of Jackson Libary for JSON Processing

If you are using Jackson to parse a JSON String in Java first time and wondering where can you get the Jackson-related JARs, or which JAR files are really need then you have come to the right place. It can be really confusing for a new user to set up or include Jackson library in their Java projects sometimes, especially, if you are not very familiar with the structure of Jackson projects, various JAR files, and in particular Maven. Since Jackson is the most popular open-source library for processing JSON responses e.g. generating Java objects from JSON received from RESTful Web services or generating JSON String from Plain Old Java Objects (POJO). It is the go-to library for any JSON need because it offers a lot of features without compromising on performance.

You can pretty-print your JSON for debugging and testing purposes (see), you can ignore null values, you can handle unknown properties, not yet defined in your POJO but coming on JSON and can do a lot more, but for all that you need to be required Jackson JAR files.

The Jackson library is divided into three different packages, Jackson Databind, Jackson Core, and Jackson Annotations. The Jackson Databind package provides general data-binding functionality for Jackson and also required for streaming API.

The Jackson core package contains core Jackson abstractions and basic JSON streaming implementations, and as the name suggests, the Jackson Annotations package contains core annotations used for value types and used by the Jackson data binding package. The Jackson core and annotations are required dependency for Jackson data-bind package.

In short, If you include jackson-databind dependency or jackson-databind.jar in your classpath, you will also need jackson-core.jar and jackson-annotations.jar files with the compatible or same version. The current stable version of the Jackson library is version 2.8

You should learn Jackson better to become a better Java developer. And, If you need a resource, check out this Jackson Quick Start: JSON Serialization With Java Made Easy - a free course on Udemy to learn Jackson API basics.



Using Maven

If you are using Maven then its relatively easy to get all the required JAR files for using Jackson in your Java project. You don't need to download dependencies for Jackson library, just specify that you need jackson-databind dependency and Maven will take care of everything i.e. downloading dependencies for this library e.g. jackson-core and jackson-annotations.

This is the preferred approach and I highly recommend you to use Maven for dependency management if you are not using it yet. Another key advantage of this approach or Maven is that you don't need to worry about versions, Maven will automatically download the correct version of dependent JARs.

For example, if you include jackson-databind version 2.2. 3 then Maven will download jackson-annotations-2.2.3.jar, and jackson-core-2.2.3.jar files.


In short, you just to copy paste following dependency in your pom.xml file and run the mvn install command to download required JAR files for Jackson.

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.3</version>
</dependency>

This will download all required JAR files and if you are using Maven you can see them included in your build path as Maven dependency as shown in the following screenshot.

How to get JAR files of Jackson Libary for JSON Processing


Manually Downloading JAR Files

If you are not using Maven or you have any connectivity issue due to which you are not able to connect to maven central repository from your workstation or Eclipse then you can manually download Jackson JAR files from Maven Central Library.

You need three JAR files jackson-databind.jar, jackson-core.jar, and jackson-annotations.jar files. Even though jackson-core and jackson-annotations version x.y.0 is generally compatible with jackson-databind version x.y.1, x.y.2, etc.

I also recommend downloading the same version of JAR files to avoid any conflict or dependency issues. Since they are sometimes very hard to find, it's better to be safe than sorry.

In short, if you decided to use Jackson 2.2.3 version for your Java JSON project, you need following 3 JAR files
jackson-databind-2.2.3.jar,
jackson-annotations-2.2.3.jar, and
jackson-core-2.2.3.jar file.

You can download these libraries from Maven central library here.



One of the important things to remember is that there are two major versions of Jackson library i.e. Jackson 1.x (1.0 - 1.9) and Jackson 2.x (2.0 - 2.8). The main difference between these versions is that Jackson classes are located in different Java package and it uses different jar naming and Maven group/artifact ids.

This means both versions can also co-exist in the classpath, but this means that you have to make sure that all components in use have matching major versions: specifically, Jackson 2.x code does NOT understand or support Jackson 1.x annotations, or vice versa.

Minor versions (like 2.1 and 2.2) are backward compatible with respect to public API: old code should work without recompilation, if (but only if) it relies on external API i.e. public methods.


That's all about which Jackson JAR file to download and how to include Jackson dependencies in the classpath. As I said, the jackson-databind.jar is the real Jackson library but it is dependent upon jackson-core.jar and jackson-annotations.jar files. 

When you use Maven to download Jackson library, you just need to add the jackson-databind on dependency and it will automatically download the jackson-core and jackson-annotations because jackson-databind depends upon them. This is called as transitive dependency management of Maven.

So, if you manually download Jackson jar files for your Java JSON project, you must download the same version of jackson-databind, jackson-core, and jackson-annotations JAR files e.g. jackson-databind-2.2.3.jar, jackson-annotations-2.2.3.jar, and jackson-core-2.2.3.jar file.


Other JSON tutorials you may like to explore
  • How to convert a JSON  String to POJO in Java? (tutorial)
  • 3 Ways to parse JSON String in Java? (tutorial)
  • How to convert JSON array to String array in Java? (example)
  • How to convert a Map to JSON in Java? (tutorial)
  • How to use Google Protocol Buffer in Java? (tutorial)
  • How to use Gson to convert JSON to Java Object? (example)
  • 5 Books to Learn REST and RESTful Web Services (books)

Thanks for reading this article so far. If you find the information given in this article useful then please share it with your friends and colleagues. If you have any questions, suggestions, or feedback then please drop a note. 

P.S. - If you want to learn how to develop RESTful Web Services using Spring Framework, check out Eugen Paraschiv's REST with Spring course. He has recently launched the certification version of the course, which is full of exercises and examples to further cement the real world concepts you will learn from the course.

2 comments :

yogi said...

Thanks for the help.
I am new to using other libraries and really found this article very useful.

Anonymous said...

Your website doesn't have enough advertising. Too easy to find the content I was looking for without being annoyed.

Post a Comment