MOXy – Unmarshal JSON to Java Object

This JAXB tutorial demonstrates using MOXy to convert JSON String to Java Object. MOXy implements JAXB, allowing developers to provide their mapping information through annotations and provide many rich features that JAXB doesn’t provide by default.

1. MOXy Dependency

Include the latest version of MOXy to project runtime.


2. Setup

2.1. Add JAXB Annotations

If not done, start with adding the JAXB annotations in the class. This example uses Employee and Department classes.

@XmlRootElement(name = "employee")
public class Employee implements Serializable {

	private static final long serialVersionUID = 1L;

	private Integer id;
	private String firstName;
	private String lastName;
	private Department department;

	public Employee() {

	//Setters and Getters
@XmlRootElement(name = "department")
public class Department implements Serializable {

	private static final long serialVersionUID = 1L;

	Integer id;
	String name;

	public Department() {

	//Setters and Getters

2.2. Add

When we get the instance of JAXBContext, JAXB checks for file and constructs the context. Here, we can inject the JAXBContextFactory from MOXy library.

Place file in same package where JAXB annotated classes are placed.


3. Unmarshalling JSON File to Java Object

Now use javax.xml.bind.UnMarshaller class to convert JSON to POJO.

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.eclipse.persistence.jaxb.MarshallerProperties;
import org.eclipse.persistence.jaxb.UnmarshallerProperties;
import com.howtodoinjava.demo.model.Department;
import com.howtodoinjava.demo.model.Employee;

public class JaxbExample
	public static void main(String[] args)
		String fileName = "employee.json";

	private static void jaxbJsonToObject(String fileName) {
		File xmlFile = new File(fileName);

		JAXBContext jaxbContext;
			jaxbContext = JAXBContext.newInstance(Employee.class);
			Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

			//Set JSON type
			jaxbUnmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
			jaxbUnmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, true);

			Employee employee = (Employee) jaxbUnmarshaller.unmarshal(xmlFile);

		catch (JAXBException e)

Program Output:

Employee [id=1, firstName=Lokesh, lastName=Gupta, department=Department [id=101, name=IT]]

Where the JSON file is:

   "employee" : {
      "department" : {
         "id" : 101,
         "name" : "IT"
      "firstName" : "Lokesh",
      "id" : 1,
      "lastName" : "Gupta"

4. Convert JSON String to Java Object

We can get the JSON in String and then populate it to Java objects directly.

String jsonString = "{\"employee\":{\"department\":{\"id\":101,\"name\":\"IT\"},

Employee employee = (Employee) jaxbUnmarshaller.unmarshal(new StringReader(jsonString));

Program Output:

Employee [id=1, firstName=Lokesh, lastName=Gupta, department=Department [id=101, name=IT]]

Drop me your questions related to unmarshalling JSON to Java objects in the comments section.

Happy Learning !!

Leave a Reply

Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial