In this tutorial we will learn about JSON-B, which is a standard binding layer for converting Java objects to/from JSON messages, included as part of JSR 367 Specification (Java EE 8)

JSON-B defines a default mapping API for converting existing Java classes to JSON and viceversa. It brings several advantages such as:

  • Consistency with JAXB (Java API for XML Binding) and other Java EE and SE APIs where appropriate
  • Standard for mapping of Java classes and instances to JSON document counterparts
  • Customizable: Allow customization of the default mapping definition
  • Ease of use: Default use of the APIs should not require prior knowledge of the JSON document format and specification

Let's see a practical application on WildFly 12 which has initial support for Java EE 8 in a profile called standalone-ee8.xml.

$ ./ -c standalone-ee8.xml

And here is a simple REST Service which we will deploy as Web application:

package com.itbuzzpress.json.service;

import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;




import com.itbuzzpress.json.model.Person;

public class JsonService {

	public Response createJava(@FormParam("json") String json) {
		Response response;

		Jsonb jsonb = JsonbBuilder.create();

		Person p = jsonb.fromJson(json, Person.class);
		System.out.println("Created Person " + p);
		response = Response.ok(p.toString()).build();

		return response;

	public Response createJson(@FormParam("name") String name, @FormParam("surname") String surname,
			@FormParam("address") String address, @FormParam("city") String city) {
		Response response;
		Person p = new Person(name, surname, address, city);

		Jsonb jsonb = JsonbBuilder.create();
		String jsonString = jsonb.toJson(p);
		response = Response.ok(jsonString).build();

		return response;

This REST service contains two methods, the former createJava will create a Java Object from a Json String passed as Form parameter and output the toString() of the Java object as response.

The main class used in this example is javax.json.bind.Jsonb. This class provides all necessary methods to serialize and deserialize Java objects.

In the first method we use the static fromJson method to create a JSON from a Java Object

In the second method we instead use the toJson to do exactly the opposite, that is converting from a Java Object to a JSON String.

Please note that Jsonb instances are thread safe. They can be cached and reused.

A minimal JSP will be created as front-end for our application:

<%@ page language="java" contentType="text/html; charset=UTF-8"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
	<h2>Enter Properties for Person</h2>
	<form method="post" action="rest/jsonb/tojson">
		<label>Name</label><br /> <input type="text" name="name"><br />
		<label>Surname</label><br /> <input type="text" name="surname"><br />
		<label>Address</label><br /> <input type="text" name="address"><br />
		<label>City</label><br /> <input type="text" name="city"><br />
		<label> </label> <br /> <input type="submit" value="Create Json">

	<br />
	<h2>Enter Json for Object "Person" here:</h2>
	<h3>Example: { "name": "John", "surname": "Doe", "address": "77 E
		4th St", "city": "New York" }</h3>
	<form method="post" action="rest/jsonb/tojava">
		Json<br /> <input type="text" name="json" width="400" /> <br> <br />
		<input type="submit" value="Create Java">

Here is out application in action:

JSON-B tutorial java ee 8 jakarta ee wildfly jboss

The example is available on my repository and it will be included in Practical Java EE development I'm currently updating.

Mapping a collection

In the above example we have mapped a single Java object to Json, however JSON-B supports also collections and generic collections handling. It can be done a way shown below:

List<Person> list = new ArrayList<>();
Person p1 = new Person(name1, surname1, address1, city1);
Person p2 = new Person(name2, surname2, address2, city2);


Jsonb jsonb = JsonbBuilder.create();
String result = jsonb.toJson(list);

// Deserialize back
list = jsonb.fromJson(result, new ArrayList<Person>(){}.getClass().getGenericSuperclass());

Customized mapping

Json mappings can be customized in several ways. You can use JSON-B annotations for compile time customizations and JsonbConfig class for runtime customizations.

For example, the Person class can be customized using the @JsonbProperty to specify a custom naming policy:

public class Person {
    private String name;


This would result in the following Json:

{"address":"77 E 4th St","city":"New York","first-name":"John","surname":"Doe"}

Among the other features, it is also possible the creation of custom configuration by skipping elements with null values such as in the following example:


// Create custom configuration
JsonbConfig nillableConfig = new JsonbConfig().withNullValues(true);

// Create Jsonb with custom configuration
Jsonb jsonb = JsonbBuilder.create(nillableConfig);

// Use it!
String result = jsonb.toJson(person);

That's all! Enjoy JSON-B Api!