4 JSON Libraries Every Java Developer Should Know About

Gson, Jackson, Primefaces and JSON-P

As exposing and consuming web services becomes our everyday life especially with JAX-RS 2.0 in Java EE 7 making exposing web services a piece of cake for Java developers, let’s talk about JSON (Javascript Object Notation) which has gradually become arguably the most used format of data exchange in web services.

It might sound unbelievable but the truth is many developers especially very young ones still generate JSON by concatenating strings if they even use JSON at all i.e

In this post I will examine 4 JSON processing libraries in Java that I’ve found myself using at one time or the other over the years.

I created 2 simple single-file Netbeans project (An Ant and a Maven project) to demostrate how to configure these libraries in your project and demostrate their basic usage. I have provided links to some more useful resources if you wish to deepen your knowledge.

Now let's walk through the libraries and see how to configure each in our next project.

1. GSON by Google

Gson by Google is one of the most popular libraries for JSON processing. Gson does conversion of java objects to json and vice-versa quite well. You can also use it to generate JSON from just about any arbitrary data.To use Gson in your next project, download the latest version from Maven Central Repository, extract the gson-2.7.jar to your classpath or if you’re a maven fan, setup Gson by adding the code below to your POM file:

If you're writing a JSF (Java Server Faces) application with Primefaces, there might not be need to use any external JSON library as Primeafces is bundled with a very handy JSON package:

org.primefaces.json

Also if you're using NetBeans IDE for non-JSF projects, Primefaces libraries are already bundled with NetBeans so you can just simply add it as a library to your project or download it at the official download page. For Maven projects, below is the dependency snippet:

A caveat for Primeafces JSON library is the nasty exception it throws if you try to access a field that does not exist in the JSON data. So you must either be sure of the fields or handle JSONException properly.

Just as you dont need to add external JSON library to your JSF/Primeafces project, you dont need to add any external library for JSON processing for your next Java EE 7 projects. If you're not writing a Java EE 7, then download the jar file at https://repo1.maven.org/maven2/org/glassfish/javax.json/ or setup your maven project with the dependency below: