How to convert JSON array to Java POJO using Jackson

Priya Salvi
3 min readJun 18, 2024

--

To convert a JSON array into a list of custom Java objects using Jackson, follow these steps:

  1. Add Jackson Dependency: Ensure you have Jackson library included in your project dependencies.

Maven:

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
</dependency>

Gradle:

implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.3'

Example:

Address.java

public class Address {
private String apartment;
private String street;
private String pinCode;

// Getters and setters
public String getApartment() {
return apartment;
}

public void setApartment(String apartment) {
this.apartment = apartment;
}

public String getStreet() {
return street;
}

public void setStreet(String street) {
this.street = street;
}

public String getPinCode() {
return pinCode;
}

public void setPinCode(String pinCode) {
this.pinCode = pinCode;
}
}

Person.java

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Person {
@JsonProperty("name")
@JsonInclude(JsonInclude.Include.NON_NULL)
private String name;

@JsonProperty("age")
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
private int age;

@JsonProperty("address")
private List<Address> address;

// Getters and setters
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public List<Address> getAddress() {
return address;
}

public void setAddress(List<Address> address) {
this.address = address;
}
}

Main Class to Convert JSON to List of Person Objects:

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.List;

public class Main {
public static void main(String[] args) {
String jsonArray = "[{\"name\":\"John\", \"age\":30, \"address\":[{\"apartment\":\"A1\",\"street\":\"Main St\",\"pinCode\":\"12345\"}], \"unknownField\":\"value\"}, {\"name\":\"Alice\", \"age\":25, \"address\":[{\"apartment\":\"B2\",\"street\":\"Second St\",\"pinCode\":\"67890\"}]}]";

try {
ObjectMapper objectMapper = new ObjectMapper();
List<Person> personList = objectMapper.readValue(jsonArray, new TypeReference<List<Person>>() {});

for (Person person : personList) {
System.out.println("Name: " + person.getName() + ", Age: " + person.getAge());
for (Address address : person.getAddress()) {
System.out.println(" Apartment: " + address.getApartment() + ", Street: " + address.getStreet() + ", PinCode: " + address.getPinCode());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Common Errors and Fixes

  1. UnrecognizedPropertyException:
  • Error: Occurs when JSON contains fields not present in the POJO.
  • Fix: Ignore unknown properties using @JsonIgnoreProperties(ignoreUnknown = true).

2. JsonMappingException:

  • Error: Occurs when there’s a mismatch between JSON structure and Java POJO.
  • Fix: Ensure JSON keys match POJO field names or use @JsonProperty annotation to map JSON keys to POJO fields.

3. InvalidFormatException:

  • Error: Occurs when JSON value types do not match POJO field types.
  • Fix: Ensure JSON values match the expected types in POJO.

4. Missing required properties:

  • Error: Occurs when JSON lacks fields that are required in the POJO.
  • Fix: Ensure JSON includes all necessary fields or make fields optional in POJO using @JsonInclude.

Example with Fixes Applied

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.List;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Person {
@JsonProperty("name")
@JsonInclude(JsonInclude.Include.NON_NULL)
private String name;

@JsonProperty("age")
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
private int age;

// Getters and setters
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
}

public class Main {
public static void main(String[] args) {
String jsonArray = "[{\"name\":\"John\", \"age\":30, \"unknownField\":\"value\"}, {\"name\":\"Alice\", \"age\":25}]";

try {
ObjectMapper objectMapper = new ObjectMapper();
List<Person> personList = objectMapper.readValue(jsonArray, new TypeReference<List<Person>>() {});

for (Person person : personList) {
System.out.println("Name: " + person.getName() + ", Age: " + person.getAge());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

In this example:

  • The @JsonIgnoreProperties(ignoreUnknown = true) annotation ensures that any unknown properties in the JSON are ignored, preventing UnrecognizedPropertyException.
  • The TypeReference<List<Person>>(){} is used to inform Jackson about the type information for deserialization, enabling it to convert the JSON array into a list of Person objects.

--

--

Priya Salvi

Software Engineer | Oracle Certified Java Associate | Neophile | Bibliophile