Javaexercise.com

How to convert JSON to an object in Java

In this article, we will learn to convert JSON data to Java object to read data in a Java-style way.

We will use the Jackson library to read and write in Java.

This library provides several classes, interfaces, and tons of utility methods to work with JSON handling in Java.

The ObjectMapper class of Jackson is one of the useful classes that provide functionality for reading and writing JSON in Java.

Mapper instances are fully thread-safe provided that ALL configuration of the instance occurs before ANY read or write calls.

Let's start with running examples.

Read JSON file in Java - Convert  JSON to a Java Object

This is the JSON file, we are going to read and convert to a Java object.

//studentfile.json

{
	"firstName" : "Rohan",
	"lastName" : "Singh",
	"dateOfBirth" : "1990-12-15",
	"address" : "34/Art",
	"city" : "California",
	"contact" : "85652321"
}

This is the code to convert JSON to java code. here, we used ObjectMapper class and its method readValue() to read data from the JSON file.

It returns an object that further can be used to access data using getter methods. See the Java code below.

package myjavaproject;
import java.io.File;
import java.io.IOException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
/* 
 * Example to convert a JSON into Java object(values) in Java 
 */
public class JExercise {
	public static void main(String[] args) {
		ObjectMapper mapper = new ObjectMapper();
		mapper.enable(SerializationFeature.INDENT_OUTPUT);
		try {
			Student student = mapper.readValue(new File("studentfile.json"), Student.class);
			System.out.println("First Name: "+student.getFirstName());
			System.out.println("Last Name: "+student.getLastName());
			System.out.println("City: "+student.getCity());
			System.out.println("Address: "+student.getAddress());
			System.out.println("Contact: "+student.getContact());
			System.out.println("Date of Birth: "+student.getDateOfBirth());
		}catch(IOException e) {
			e.printStackTrace();
		}
	}
}

Output:

First Name: Jacob
Last Name: David
City: California
Address: 2B-234 street-A
Contact: 5236214523
Date of Birth: 1998-12-10
 

This is our POJO class that basically used for mapping the JSON data and getting converted to the object.

If you noticed the readValue() method, we passed the Student class as the second argument(readValue(new File("write-json.json"), Student.class)) there to get student type object.

// Student.java

package myjavaproject;

class Student{
	private String firstName;
	private String lastName;
	private String dateOfBirth;
	private String address;
	private String city;
	private String contact;

	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getDateOfBirth() {
		return dateOfBirth;
	}
	public void setDateOfBirth(String dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getContact() {
		return contact;
	}
	public void setContact(String contact) {
		this.contact = contact;
	}
}

 

Note: Make sure all the above files are placed in the same folder, else you may get compile errors.