Best JSON Library for Java

In this post, I would like to discuss the most common JSON processing libraries in Java.

Working with JSON data in Java can be easy, but – like most anything in Java – there are a lot of options and libraries we can choose from.

As per my experience, I have used below most popular  JSON processing libraries to work with Java:
Let's briefly see the overview of each of this JSON library and I have written a complete tutorial for above Libraries (link provided under each section).

Jackson

Jackson is a very popular and efficient Java-based library to serialize or map Java objects to JSON and vice versa. This tutorial contains a large number of articles/posts which demonstrates the basic and advanced Jackson library API features and their usage with lots of examples.

Jackson offers three methods for processing JSON format, each of them having its pros and cons:
  • Streaming API or incremental parsing/generation: reads and writes JSON content as discrete events
  • Tree model: provides a mutable in-memory tree representation of a JSON document
  • Data binding: converts JSON to and from POJO.
Check out complete tutorials and examples of Jackson library at Java Jackson JSON Tutorial with Examples.

GSON

Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object.
Gson can work with arbitrary Java objects including pre-existing objects that you do not have a source code of. The following tutorials will demonstrate how you can leverage GSON to manage your JSON conversions.
Gson was originally created for use inside Google where it is currently used in a number of projects. It is now used by a number of public projects and companies.

Goals for Gson

  • Provide easy to use mechanisms like toString() and constructor (factory method) to convert Java to JSON and vice-versa
  • Allow pre-existing unmodifiable objects to be converted to and from JSON
  • Allow custom representations for objects
  • Support arbitrarily complex objects
  • Generate compact and readable JSON output
Check out complete tutorials and examples of Google GSON library at Google GSON Tutorial.

JSON.simple

JSON.simple is a simple Java library for JSON processing, read and write JSON data and full compliance with JSON specification (RFC4627).

The JSON-simple is one of the simplest JSON library, also lightweight. You can use this library to encode or decode JSON text. It's an open-source lightweight library which is flexible and easy to be used by reusing Map and List interfaces from JDK. A good thing about this library that it has no external dependency.

Check out complete tutorials and examples of JSON.simple library at JSON.simple Tutorial - Read and Write JSON in Java.

JSON-P

Java JSON processing tutorial shows how to use JSON-P library to work with JSON. 
JSR 353 addresses the standard Java API for JSON processing and shipped as part of JavaEE 7. Java API for JSON Processing (JSON-P) provides portable APIs to parse, generate, transform, and query JSON using object model and streaming APIs. 
There are two ways two work with JSON in JSON-P: streaming API and object model API.
The streaming API hands over parsing and generation control to the programmer. The streaming API provides an event-based parser and allows an application developer to ask for the next event rather than handling the event in a callback. This is called a pull method.
NameDescription
JsonContains static methods to create JSON parsers, generators, and their factories.
JsonParserRepresents an event-based parser reads JSON data from a stream.
JsonGeneratorWrites JSON data to a stream one value at a time.
The object model API creates a tree-like structure that represents the JSON data in memory. The tree can be flexibly navigated and queried. On the other hand, the object model API is often less efficient as the streaming model and requires more memory.
NameDescription
JsonContains static methods to create JSON parsers, generators, and their factories.
JsonObjectBuilderCreates an object model in memory by adding values from application code.
JsonArrayBuilderCreates an array model in memory by adding values from application code.
JsonReaderReads a JsonObject or a JsonArray from an input source.
JsonWriterWrites a JsonObject or a JsonArray to an output source.
The JsonValueJsonObjectJsonArrayJsonString, and JsonNumber are JSON data types.

Check out complete tutorials and examples of JSON-P library at Java JSON Processing Tutorial.

References

Comments